blob: 2dc9a6a2bc53329d6b69b7f9b6645aeb959b747d [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 Yamasanie928d7d2012-09-17 21:46:51 -070032import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
Amith Yamasani258848d2012-08-10 17:06:33 -070034import android.os.Binder;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070035import android.os.Environment;
36import android.os.FileUtils;
Amith Yamasani258848d2012-08-10 17:06:33 -070037import android.os.IUserManager;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070038import android.os.ParcelFileDescriptor;
Amith Yamasani258848d2012-08-10 17:06:33 -070039import android.os.Process;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070040import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070041import android.os.UserHandle;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070042import android.os.UserManager;
Amith Yamasani2a003292012-08-14 18:25:45 -070043import android.util.AtomicFile;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070044import android.util.Slog;
45import android.util.SparseArray;
46import android.util.Xml;
47
48import java.io.BufferedOutputStream;
49import java.io.File;
50import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070051import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070052import java.io.FileOutputStream;
53import java.io.IOException;
54import java.util.ArrayList;
55import java.util.List;
56
57import org.xmlpull.v1.XmlPullParser;
58import org.xmlpull.v1.XmlPullParserException;
59import org.xmlpull.v1.XmlSerializer;
60
Amith Yamasani258848d2012-08-10 17:06:33 -070061public class UserManagerService extends IUserManager.Stub {
Amith Yamasanib8151ec2012-04-18 18:02:48 -070062
Amith Yamasani2a003292012-08-14 18:25:45 -070063 private static final String LOG_TAG = "UserManagerService";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070064
Amith Yamasani4b2e9342011-03-31 12:38:53 -070065 private static final String TAG_NAME = "name";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070066 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070067 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070068 private static final String ATTR_ID = "id";
Amith Yamasani2a003292012-08-14 18:25:45 -070069 private static final String ATTR_SERIAL_NO = "serialNumber";
70 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070071 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070072 private static final String TAG_USER = "user";
73
Amith Yamasani0b285492011-04-14 17:35:23 -070074 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070075 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -070076 private static final String USER_PHOTO_FILENAME = "photo.png";
Amith Yamasani4b2e9342011-03-31 12:38:53 -070077
Dianne Hackborn4428e172012-08-24 17:43:05 -070078 private final Context mContext;
79 private final PackageManagerService mPm;
80 private final Object mInstallLock;
81 private final Object mPackagesLock;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070082
83 private final File mUsersDir;
84 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -070085 private final File mBaseUserPath;
86
87 private SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
88
Amith Yamasani0b285492011-04-14 17:35:23 -070089 private int[] mUserIds;
Amith Yamasani258848d2012-08-10 17:06:33 -070090 private boolean mGuestEnabled;
Amith Yamasani2a003292012-08-14 18:25:45 -070091 private int mNextSerialNumber;
Amith Yamasani0b285492011-04-14 17:35:23 -070092
Amith Yamasani258848d2012-08-10 17:06:33 -070093 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070094
Dianne Hackborn4428e172012-08-24 17:43:05 -070095 public static UserManagerService getInstance() {
96 synchronized (UserManagerService.class) {
97 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -070098 }
Amith Yamasani258848d2012-08-10 17:06:33 -070099 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700100
101 /**
102 * Available for testing purposes.
103 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700104 UserManagerService(File dataDir, File baseUserPath) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700105 this(null, null, new Object(), new Object(), dataDir, baseUserPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700106 }
107
Dianne Hackborn4428e172012-08-24 17:43:05 -0700108 /**
109 * Called by package manager to create the service. This is closely
110 * associated with the package manager, and the given lock is the
111 * package manager's own lock.
112 */
113 UserManagerService(Context context, PackageManagerService pm,
114 Object installLock, Object packagesLock) {
115 this(context, pm, installLock, packagesLock,
116 Environment.getDataDirectory(),
117 new File(Environment.getDataDirectory(), "user"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700118 }
119
Dianne Hackborn4428e172012-08-24 17:43:05 -0700120 /**
121 * Available for testing purposes.
122 */
123 private UserManagerService(Context context, PackageManagerService pm,
124 Object installLock, Object packagesLock,
125 File dataDir, File baseUserPath) {
126 synchronized (UserManagerService.class) {
127 mContext = context;
128 mPm = pm;
129 mInstallLock = installLock;
130 mPackagesLock = packagesLock;
131 mUsersDir = new File(dataDir, USER_INFO_DIR);
132 mUsersDir.mkdirs();
133 // Make zeroth user directory, for services to migrate their files to that location
134 File userZeroDir = new File(mUsersDir, "0");
135 userZeroDir.mkdirs();
136 mBaseUserPath = baseUserPath;
137 FileUtils.setPermissions(mUsersDir.toString(),
138 FileUtils.S_IRWXU|FileUtils.S_IRWXG
139 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
140 -1, -1);
141 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
142 readUserList();
143 sInstance = this;
144 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700145 }
146
147 @Override
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700148 public List<UserInfo> getUsers() {
Amith Yamasani2a003292012-08-14 18:25:45 -0700149 checkManageUsersPermission("query users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700150 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700151 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
152 for (int i = 0; i < mUsers.size(); i++) {
153 users.add(mUsers.valueAt(i));
154 }
155 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700156 }
Amith Yamasani13593602012-03-22 16:16:17 -0700157 }
158
Amith Yamasani258848d2012-08-10 17:06:33 -0700159 @Override
160 public UserInfo getUserInfo(int userId) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700161 checkManageUsersPermission("query user");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700162 synchronized (mPackagesLock) {
Amith Yamasani195263742012-08-21 15:40:12 -0700163 return getUserInfoLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700164 }
165 }
166
Amith Yamasani195263742012-08-21 15:40:12 -0700167 /*
168 * Should be locked on mUsers before calling this.
169 */
170 private UserInfo getUserInfoLocked(int userId) {
171 return mUsers.get(userId);
172 }
173
Amith Yamasani13593602012-03-22 16:16:17 -0700174 public boolean exists(int userId) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700175 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700176 return ArrayUtils.contains(mUserIds, userId);
177 }
178 }
179
Amith Yamasani258848d2012-08-10 17:06:33 -0700180 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700181 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700182 checkManageUsersPermission("rename users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700183 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700184 UserInfo info = mUsers.get(userId);
185 if (name != null && !name.equals(info.name)) {
186 info.name = name;
187 writeUserLocked(info);
188 }
189 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700190 sendUserInfoChangedBroadcast(userId);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700191 }
192
Amith Yamasani258848d2012-08-10 17:06:33 -0700193 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700194 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700195 checkManageUsersPermission("update users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700196 synchronized (mPackagesLock) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700197 UserInfo info = mUsers.get(userId);
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700198 if (info == null) return;
199 writeBitmapLocked(info, bitmap);
200 writeUserLocked(info);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700201 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700202 sendUserInfoChangedBroadcast(userId);
203 }
204
205 private void sendUserInfoChangedBroadcast(int userId) {
206 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
207 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
208 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
209 mContext.sendBroadcastAsUser(changedIntent, new UserHandle(userId));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700210 }
211
Amith Yamasani258848d2012-08-10 17:06:33 -0700212 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700213 public Bitmap getUserIcon(int userId) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700214 checkManageUsersPermission("read users");
215 synchronized (mPackagesLock) {
216 UserInfo info = mUsers.get(userId);
217 if (info == null || info.iconPath == null) return null;
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700218 return BitmapFactory.decodeFile(info.iconPath);
Amith Yamasani3b49f072012-09-17 10:21:43 -0700219 }
220 }
221
222 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700223 public void setGuestEnabled(boolean enable) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700224 checkManageUsersPermission("enable guest users");
Dianne Hackborn4428e172012-08-24 17:43:05 -0700225 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700226 if (mGuestEnabled != enable) {
227 mGuestEnabled = enable;
228 // Erase any guest user that currently exists
229 for (int i = 0; i < mUsers.size(); i++) {
230 UserInfo user = mUsers.valueAt(i);
231 if (user.isGuest()) {
232 if (!enable) {
233 removeUser(user.id);
234 }
235 return;
236 }
237 }
238 // No guest was found
239 if (enable) {
240 createUser("Guest", UserInfo.FLAG_GUEST);
241 }
242 }
243 }
244 }
245
246 @Override
247 public boolean isGuestEnabled() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700248 synchronized (mPackagesLock) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700249 return mGuestEnabled;
250 }
251 }
252
253 @Override
254 public void wipeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700255 checkManageUsersPermission("wipe user");
Amith Yamasani258848d2012-08-10 17:06:33 -0700256 // TODO:
257 }
258
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700259 public void makeInitialized(int userId) {
260 checkManageUsersPermission("makeInitialized");
261 synchronized (mPackagesLock) {
262 UserInfo info = mUsers.get(userId);
263 if (info != null && (info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
264 info.flags |= UserInfo.FLAG_INITIALIZED;
265 writeUserLocked(info);
266 }
267 }
268 }
269
Amith Yamasani258848d2012-08-10 17:06:33 -0700270 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700271 * Check if we've hit the limit of how many users can be created.
272 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700273 private boolean isUserLimitReachedLocked() {
274 int nUsers = mUsers.size();
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700275 return nUsers >= UserManager.getMaxSupportedUsers();
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700276 }
277
278 /**
Amith Yamasani195263742012-08-21 15:40:12 -0700279 * Enforces that only the system UID or root's UID or apps that have the
280 * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS}
281 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -0700282 *
283 * @param message used as message if SecurityException is thrown
284 * @throws SecurityException if the caller is not system or root
285 */
Amith Yamasani2a003292012-08-14 18:25:45 -0700286 private static final void checkManageUsersPermission(String message) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700287 final int uid = Binder.getCallingUid();
Amith Yamasani2a003292012-08-14 18:25:45 -0700288 if (uid != Process.SYSTEM_UID && uid != 0
289 && ActivityManager.checkComponentPermission(
290 android.Manifest.permission.MANAGE_USERS,
291 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
292 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
Amith Yamasani258848d2012-08-10 17:06:33 -0700293 }
294 }
295
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700296 private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700297 try {
298 File dir = new File(mUsersDir, Integer.toString(info.id));
299 File file = new File(dir, USER_PHOTO_FILENAME);
300 if (!dir.exists()) {
301 dir.mkdir();
302 FileUtils.setPermissions(
303 dir.getPath(),
304 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
305 -1, -1);
306 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700307 FileOutputStream os;
308 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) {
Amith Yamasani3b49f072012-09-17 10:21:43 -0700309 info.iconPath = file.getAbsolutePath();
310 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700311 try {
312 os.close();
313 } catch (IOException ioe) {
314 // What the ... !
315 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700316 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700317 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700318 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700319 }
320
Amith Yamasani0b285492011-04-14 17:35:23 -0700321 /**
322 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
323 * cache it elsewhere.
324 * @return the array of user ids.
325 */
Dianne Hackborn1676c852012-09-10 14:52:30 -0700326 public int[] getUserIds() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700327 synchronized (mPackagesLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700328 return mUserIds;
329 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700330 }
331
Dianne Hackborn4428e172012-08-24 17:43:05 -0700332 int[] getUserIdsLPr() {
333 return mUserIds;
334 }
335
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700336 private void readUserList() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700337 synchronized (mPackagesLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700338 readUserListLocked();
339 }
340 }
341
342 private void readUserListLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -0700343 mGuestEnabled = false;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700344 if (!mUserListFile.exists()) {
Amith Yamasani13593602012-03-22 16:16:17 -0700345 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700346 return;
347 }
348 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700349 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700350 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700351 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700352 XmlPullParser parser = Xml.newPullParser();
353 parser.setInput(fis, null);
354 int type;
355 while ((type = parser.next()) != XmlPullParser.START_TAG
356 && type != XmlPullParser.END_DOCUMENT) {
357 ;
358 }
359
360 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700361 Slog.e(LOG_TAG, "Unable to read user list");
Amith Yamasani13593602012-03-22 16:16:17 -0700362 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700363 return;
364 }
365
Amith Yamasani2a003292012-08-14 18:25:45 -0700366 mNextSerialNumber = -1;
367 if (parser.getName().equals(TAG_USERS)) {
368 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
369 if (lastSerialNumber != null) {
370 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
371 }
372 }
373
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700374 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
375 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
376 String id = parser.getAttributeValue(null, ATTR_ID);
377 UserInfo user = readUser(Integer.parseInt(id));
378 if (user != null) {
379 mUsers.put(user.id, user);
Amith Yamasani2a003292012-08-14 18:25:45 -0700380 if (user.isGuest()) {
381 mGuestEnabled = true;
382 }
383 if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
384 mNextSerialNumber = user.id + 1;
385 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700386 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700387 }
388 }
Amith Yamasani13593602012-03-22 16:16:17 -0700389 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700390 } catch (IOException ioe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700391 fallbackToSingleUserLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700392 } catch (XmlPullParserException pe) {
Amith Yamasani13593602012-03-22 16:16:17 -0700393 fallbackToSingleUserLocked();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800394 } finally {
395 if (fis != null) {
396 try {
397 fis.close();
398 } catch (IOException e) {
399 }
400 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700401 }
402 }
403
Amith Yamasani13593602012-03-22 16:16:17 -0700404 private void fallbackToSingleUserLocked() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700405 // Create the primary user
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700406 UserInfo primary = new UserInfo(0, "Primary", null,
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700407 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
408 mUsers.put(0, primary);
Amith Yamasani13593602012-03-22 16:16:17 -0700409 updateUserIdsLocked();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700410
Amith Yamasani13593602012-03-22 16:16:17 -0700411 writeUserListLocked();
412 writeUserLocked(primary);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700413 }
414
415 /*
416 * Writes the user file in this format:
417 *
418 * <user flags="20039023" id="0">
419 * <name>Primary</name>
420 * </user>
421 */
Amith Yamasani13593602012-03-22 16:16:17 -0700422 private void writeUserLocked(UserInfo userInfo) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700423 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700424 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700425 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700426 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700427 final BufferedOutputStream bos = new BufferedOutputStream(fos);
428
429 // XmlSerializer serializer = XmlUtils.serializerInstance();
430 final XmlSerializer serializer = new FastXmlSerializer();
431 serializer.setOutput(bos, "utf-8");
432 serializer.startDocument(null, true);
433 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
434
435 serializer.startTag(null, TAG_USER);
436 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -0700437 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700438 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700439 if (userInfo.iconPath != null) {
440 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
441 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700442
443 serializer.startTag(null, TAG_NAME);
444 serializer.text(userInfo.name);
445 serializer.endTag(null, TAG_NAME);
446
447 serializer.endTag(null, TAG_USER);
448
449 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700450 userFile.finishWrite(fos);
451 } catch (Exception ioe) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700452 Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -0700453 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700454 }
455 }
456
457 /*
458 * Writes the user list file in this format:
459 *
Amith Yamasani2a003292012-08-14 18:25:45 -0700460 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700461 * <user id="0"></user>
462 * <user id="2"></user>
463 * </users>
464 */
Amith Yamasani13593602012-03-22 16:16:17 -0700465 private void writeUserListLocked() {
Amith Yamasani742a6712011-05-04 14:49:28 -0700466 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -0700467 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700468 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700469 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700470 final BufferedOutputStream bos = new BufferedOutputStream(fos);
471
472 // XmlSerializer serializer = XmlUtils.serializerInstance();
473 final XmlSerializer serializer = new FastXmlSerializer();
474 serializer.setOutput(bos, "utf-8");
475 serializer.startDocument(null, true);
476 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
477
478 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -0700479 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700480
481 for (int i = 0; i < mUsers.size(); i++) {
482 UserInfo user = mUsers.valueAt(i);
483 serializer.startTag(null, TAG_USER);
484 serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
485 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700486 }
487
488 serializer.endTag(null, TAG_USERS);
489
490 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -0700491 userListFile.finishWrite(fos);
492 } catch (Exception e) {
493 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -0700494 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700495 }
496 }
497
498 private UserInfo readUser(int id) {
499 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -0700500 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700501 String name = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700502 String iconPath = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700503
504 FileInputStream fis = null;
505 try {
Amith Yamasani2a003292012-08-14 18:25:45 -0700506 AtomicFile userFile =
507 new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
508 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700509 XmlPullParser parser = Xml.newPullParser();
510 parser.setInput(fis, null);
511 int type;
512 while ((type = parser.next()) != XmlPullParser.START_TAG
513 && type != XmlPullParser.END_DOCUMENT) {
514 ;
515 }
516
517 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700518 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700519 return null;
520 }
521
522 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
523 String storedId = parser.getAttributeValue(null, ATTR_ID);
524 if (Integer.parseInt(storedId) != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700525 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700526 return null;
527 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700528 String serialNumberValue = parser.getAttributeValue(null, ATTR_SERIAL_NO);
529 if (serialNumberValue != null) {
530 serialNumber = Integer.parseInt(serialNumberValue);
531 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700532 String flagString = parser.getAttributeValue(null, ATTR_FLAGS);
533 flags = Integer.parseInt(flagString);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700534 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700535
536 while ((type = parser.next()) != XmlPullParser.START_TAG
537 && type != XmlPullParser.END_DOCUMENT) {
538 }
539 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
540 type = parser.next();
541 if (type == XmlPullParser.TEXT) {
542 name = parser.getText();
543 }
544 }
545 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700546
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700547 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700548 userInfo.serialNumber = serialNumber;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700549 return userInfo;
550
551 } catch (IOException ioe) {
552 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -0800553 } finally {
554 if (fis != null) {
555 try {
556 fis.close();
557 } catch (IOException e) {
558 }
559 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700560 }
561 return null;
562 }
563
Amith Yamasani258848d2012-08-10 17:06:33 -0700564 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700565 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700566 checkManageUsersPermission("Only the system can create users");
Amith Yamasanifaea76f2012-09-11 10:59:48 -0700567
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700568 final long ident = Binder.clearCallingIdentity();
569 final UserInfo userInfo;
570 try {
571 synchronized (mInstallLock) {
572 synchronized (mPackagesLock) {
573 if (isUserLimitReachedLocked()) return null;
574 int userId = getNextAvailableIdLocked();
575 userInfo = new UserInfo(userId, name, null, flags);
576 File userPath = new File(mBaseUserPath, Integer.toString(userId));
577 userInfo.serialNumber = mNextSerialNumber++;
578 mUsers.put(userId, userInfo);
579 writeUserListLocked();
580 writeUserLocked(userInfo);
581 updateUserIdsLocked();
582 mPm.createNewUserLILPw(userId, userPath);
583 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700584 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700585 if (userInfo != null) {
586 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
587 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
588 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
589 android.Manifest.permission.MANAGE_USERS);
590 }
591 } finally {
592 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -0700593 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700594 return userInfo;
595 }
596
Amith Yamasani0b285492011-04-14 17:35:23 -0700597 /**
598 * Removes a user and all data directories created for that user. This method should be called
599 * after the user's processes have been terminated.
600 * @param id the user's id
601 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700602 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700603 checkManageUsersPermission("Only the system can remove users");
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700604 final UserInfo user;
605 synchronized (mPackagesLock) {
606 user = mUsers.get(userHandle);
607 if (userHandle == 0 || user == null) {
608 return false;
609 }
610 }
611
612 int res;
613 try {
614 res = ActivityManagerNative.getDefault().stopUser(userHandle,
615 new IStopUserCallback.Stub() {
616 @Override
617 public void userStopped(int userId) {
618 finishRemoveUser(userId);
619 }
620 @Override
621 public void userStopAborted(int userId) {
622 }
623 });
624 } catch (RemoteException e) {
625 return false;
626 }
627
628 return res == ActivityManager.USER_OP_SUCCESS;
629 }
630
631 void finishRemoveUser(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700632 synchronized (mInstallLock) {
633 synchronized (mPackagesLock) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700634 // Cleanup package manager settings
635 mPm.cleanUpUserLILPw(userHandle);
636
637 // Remove this user from the list
638 mUsers.remove(userHandle);
639 // Remove user file
640 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
641 userFile.delete();
642 // Update the user list
643 writeUserListLocked();
644 updateUserIdsLocked();
Amith Yamasani61f57372012-08-31 12:12:28 -0700645 removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
Dianne Hackborn4428e172012-08-24 17:43:05 -0700646 }
647 }
Amith Yamasani0cd867c2012-08-22 16:45:47 -0700648
Amith Yamasani2a003292012-08-14 18:25:45 -0700649 // Let other services shutdown any activity
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700650 long ident = Binder.clearCallingIdentity();
651 try {
652 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
653 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
654 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
655 android.Manifest.permission.MANAGE_USERS);
656 } finally {
657 Binder.restoreCallingIdentity(ident);
658 }
Amith Yamasani2a003292012-08-14 18:25:45 -0700659 }
660
Amith Yamasani61f57372012-08-31 12:12:28 -0700661 private void removeDirectoryRecursive(File parent) {
662 if (parent.isDirectory()) {
663 String[] files = parent.list();
664 for (String filename : files) {
665 File child = new File(parent, filename);
666 removeDirectoryRecursive(child);
667 }
668 }
669 parent.delete();
670 }
671
Amith Yamasani2a003292012-08-14 18:25:45 -0700672 @Override
673 public int getUserSerialNumber(int userHandle) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700674 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700675 if (!exists(userHandle)) return -1;
Amith Yamasani195263742012-08-21 15:40:12 -0700676 return getUserInfoLocked(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -0700677 }
678 }
679
680 @Override
681 public int getUserHandle(int userSerialNumber) {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700682 synchronized (mPackagesLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700683 for (int userId : mUserIds) {
Amith Yamasani195263742012-08-21 15:40:12 -0700684 if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -0700685 }
686 // Not found
687 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -0700688 }
689 }
690
Amith Yamasani0b285492011-04-14 17:35:23 -0700691 /**
692 * Caches the list of user ids in an array, adjusting the array size when necessary.
693 */
Amith Yamasani13593602012-03-22 16:16:17 -0700694 private void updateUserIdsLocked() {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700695 int[] newUsers = new int[mUsers.size()];
Amith Yamasani0b285492011-04-14 17:35:23 -0700696 for (int i = 0; i < mUsers.size(); i++) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700697 newUsers[i] = mUsers.keyAt(i);
Amith Yamasani0b285492011-04-14 17:35:23 -0700698 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700699 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -0700700 }
701
702 /**
703 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -0700704 * TODO: May not be a good idea to recycle ids, in case it results in confusion
705 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -0700706 * @return
707 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700708 private int getNextAvailableIdLocked() {
Dianne Hackborn4428e172012-08-24 17:43:05 -0700709 synchronized (mPackagesLock) {
Amith Yamasani07a0ede2012-09-17 14:54:26 -0700710 int i = 10;
Amith Yamasani195263742012-08-21 15:40:12 -0700711 while (i < Integer.MAX_VALUE) {
712 if (mUsers.indexOfKey(i) < 0) {
713 break;
714 }
715 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700716 }
Amith Yamasani195263742012-08-21 15:40:12 -0700717 return i;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700718 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700719 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700720}