blob: 440d8b7b81c02c254a32237ca41132f88628cb2f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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;
18
Jeff Sharkey4c099d02015-05-15 13:45:00 -070019import static com.android.internal.util.XmlUtils.readBooleanAttribute;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070020import static com.android.internal.util.XmlUtils.readIntAttribute;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -070021import static com.android.internal.util.XmlUtils.readLongAttribute;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070022import static com.android.internal.util.XmlUtils.readStringAttribute;
Jeff Sharkey4c099d02015-05-15 13:45:00 -070023import static com.android.internal.util.XmlUtils.writeBooleanAttribute;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070024import static com.android.internal.util.XmlUtils.writeIntAttribute;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -070025import static com.android.internal.util.XmlUtils.writeLongAttribute;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070026import static com.android.internal.util.XmlUtils.writeStringAttribute;
Jeff Sharkey5217cac2015-12-20 15:34:01 -070027
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070028import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
29import static org.xmlpull.v1.XmlPullParser.START_TAG;
30
Jason parks8888c592011-01-20 22:46:41 -060031import android.Manifest;
Jeff Sharkeyef10ee02015-07-05 14:17:27 -070032import android.annotation.Nullable;
Xiaohui Chen621b3fc2015-10-02 14:41:42 -070033import android.app.ActivityManager;
Elliott Hughesf839b4f2014-09-26 12:30:47 -070034import android.app.ActivityManagerNative;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070035import android.app.AppOpsManager;
Jeff Sharkey14cbe522015-07-08 14:06:37 -070036import android.app.IActivityManager;
Jeff Sharkeybcd262d2015-06-10 09:41:17 -070037import android.content.BroadcastReceiver;
Kenny Roota02b8b02010-08-05 16:14:17 -070038import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.Context;
40import android.content.Intent;
Jeff Sharkeybcd262d2015-06-10 09:41:17 -070041import android.content.IntentFilter;
Kenny Roota02b8b02010-08-05 16:14:17 -070042import android.content.ServiceConnection;
Jeff Sharkey275e3e42015-04-24 16:10:32 -070043import android.content.pm.IPackageMoveObserver;
44import android.content.pm.PackageManager;
Jeff Sharkey14cbe522015-07-08 14:06:37 -070045import android.content.pm.ProviderInfo;
Jeff Sharkeybcd262d2015-06-10 09:41:17 -070046import android.content.pm.UserInfo;
Elliott Hughesf839b4f2014-09-26 12:30:47 -070047import android.content.res.Configuration;
Kenny Root02c87302010-07-01 08:10:18 -070048import android.content.res.ObbInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
Kenny Root02c87302010-07-01 08:10:18 -070050import android.os.Binder;
Jeff Sharkey4c099d02015-05-15 13:45:00 -070051import android.os.DropBoxManager;
Kenny Roota02b8b02010-08-05 16:14:17 -070052import android.os.Environment;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070053import android.os.Environment.UserEnvironment;
Jeff Sharkey48877892015-03-18 11:27:19 -070054import android.os.FileUtils;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080055import android.os.Handler;
Dianne Hackbornefa92b22013-05-03 14:11:43 -070056import android.os.HandlerThread;
Kenny Roota02b8b02010-08-05 16:14:17 -070057import android.os.IBinder;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040058import android.os.Looper;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080059import android.os.Message;
Daichi Hirono9e8d9e22015-11-13 14:37:00 +090060import android.os.ParcelFileDescriptor;
Jeff Sharkeyce14cd02015-12-07 15:35:42 -070061import android.os.PowerManager;
Jeff Sharkey9527b222015-06-24 15:24:48 -070062import android.os.Process;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070063import android.os.RemoteCallbackList;
San Mehat4270e1e2010-01-29 05:32:19 -080064import android.os.RemoteException;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080065import android.os.ServiceManager;
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -070066import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070068import android.os.UserHandle;
Emily Bernier92aa5a22014-07-07 10:11:48 -040069import android.os.UserManager;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070070import android.os.storage.DiskInfo;
Kenny Roota02b8b02010-08-05 16:14:17 -070071import android.os.storage.IMountService;
72import android.os.storage.IMountServiceListener;
73import android.os.storage.IMountShutdownObserver;
74import android.os.storage.IObbActionListener;
Svet Ganov6ee871e2015-07-10 14:29:33 -070075import android.os.storage.MountServiceInternal;
Kenny Rootaf9d6672010-10-08 09:21:39 -070076import android.os.storage.OnObbStateChangeListener;
Paul Lawrence46791e72014-04-03 09:10:26 -070077import android.os.storage.StorageManager;
Kenny Roota02b8b02010-08-05 16:14:17 -070078import android.os.storage.StorageResultCode;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070079import android.os.storage.StorageVolume;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070080import android.os.storage.VolumeInfo;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070081import android.os.storage.VolumeRecord;
Jeff Sharkey14cbe522015-07-08 14:06:37 -070082import android.provider.MediaStore;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -070083import android.provider.Settings;
Jason parksf7b3cd42011-01-27 09:28:25 -060084import android.text.TextUtils;
Jeff Sharkey1783f142015-04-17 10:52:51 -070085import android.text.format.DateUtils;
Jeff Sharkey48877892015-03-18 11:27:19 -070086import android.util.ArrayMap;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070087import android.util.AtomicFile;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070088import android.util.Log;
San Mehata5078592010-03-25 09:36:54 -070089import android.util.Slog;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -070090import android.util.TimeUtils;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070091import android.util.Xml;
Jeff Sharkey48877892015-03-18 11:27:19 -070092
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080093import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070094import com.android.internal.app.IMediaContainerService;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070095import com.android.internal.os.SomeArgs;
Jeff Sharkey9527b222015-06-24 15:24:48 -070096import com.android.internal.os.Zygote;
Jeff Sharkey48877892015-03-18 11:27:19 -070097import com.android.internal.util.ArrayUtils;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070098import com.android.internal.util.FastXmlSerializer;
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -080099import com.android.internal.util.HexDump;
Jeff Sharkey5aca2b82013-10-16 16:21:54 -0700100import com.android.internal.util.IndentingPrintWriter;
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700101import com.android.internal.util.Preconditions;
Jeff Sharkeyb9fe5372015-12-03 15:23:08 -0700102import com.android.internal.widget.LockPatternUtils;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700103import com.android.server.NativeDaemonConnector.Command;
Jeff Sharkey56cd6462013-06-07 15:09:15 -0700104import com.android.server.NativeDaemonConnector.SensitiveArg;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700105import com.android.server.pm.PackageManagerService;
Kenny Roota02b8b02010-08-05 16:14:17 -0700106
Jeff Sharkey5217cac2015-12-20 15:34:01 -0700107import libcore.io.IoUtils;
108import libcore.util.EmptyArray;
109
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700110import org.xmlpull.v1.XmlPullParser;
111import org.xmlpull.v1.XmlPullParserException;
112import org.xmlpull.v1.XmlSerializer;
113
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700114import java.io.File;
Kenny Root38cf8862010-09-26 14:18:51 -0700115import java.io.FileDescriptor;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700116import java.io.FileInputStream;
117import java.io.FileNotFoundException;
Christopher Tate7265abe2014-11-21 13:54:45 -0800118import java.io.FileOutputStream;
Kenny Root05105f72010-09-22 17:29:43 -0700119import java.io.IOException;
Kenny Root38cf8862010-09-26 14:18:51 -0700120import java.io.PrintWriter;
Kenny Root3b1abba2010-10-13 15:00:07 -0700121import java.math.BigInteger;
Paul Lawrence8e397362014-01-27 15:22:30 -0800122import java.nio.charset.StandardCharsets;
Kenny Root735de3b2010-09-30 14:11:39 -0700123import java.security.NoSuchAlgorithmException;
Kenny Root3b1abba2010-10-13 15:00:07 -0700124import java.security.spec.InvalidKeySpecException;
125import java.security.spec.KeySpec;
San Mehat22dd86e2010-01-12 12:21:18 -0800126import java.util.ArrayList;
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -0800127import java.util.Arrays;
Kenny Roota02b8b02010-08-05 16:14:17 -0700128import java.util.HashMap;
San Mehat6cdd9c02010-02-09 14:45:20 -0800129import java.util.HashSet;
Kenny Root38cf8862010-09-26 14:18:51 -0700130import java.util.Iterator;
Kenny Roota02b8b02010-08-05 16:14:17 -0700131import java.util.LinkedList;
132import java.util.List;
Elliott Hughesf839b4f2014-09-26 12:30:47 -0700133import java.util.Locale;
Kenny Roota02b8b02010-08-05 16:14:17 -0700134import java.util.Map;
Kenny Root38cf8862010-09-26 14:18:51 -0700135import java.util.Map.Entry;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700136import java.util.Objects;
Svet Ganov6ee871e2015-07-10 14:29:33 -0700137import java.util.concurrent.CopyOnWriteArrayList;
Kenny Root51a573c2012-05-17 13:30:28 -0700138import java.util.concurrent.CountDownLatch;
139import java.util.concurrent.TimeUnit;
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -0700140import java.util.concurrent.TimeoutException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
Kenny Root3b1abba2010-10-13 15:00:07 -0700142import javax.crypto.SecretKey;
143import javax.crypto.SecretKeyFactory;
144import javax.crypto.spec.PBEKeySpec;
145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146/**
Jeff Sharkey48877892015-03-18 11:27:19 -0700147 * Service responsible for various storage media. Connects to {@code vold} to
148 * watch for and manage dynamically added storage, such as SD cards and USB mass
149 * storage. Also decides how storage should be presented to users on the device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 */
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -0700151class MountService extends IMountService.Stub
152 implements INativeDaemonConnectorCallbacks, Watchdog.Monitor {
Jason parks5af0b912010-11-29 09:05:25 -0600153
Christopher Tated417d622013-08-19 16:14:25 -0700154 // Static direct instance pointer for the tightly-coupled idle service to use
155 static MountService sSelf = null;
156
Jeff Sharkey56e62932015-03-21 20:41:00 -0700157 public static class Lifecycle extends SystemService {
158 private MountService mMountService;
159
160 public Lifecycle(Context context) {
161 super(context);
162 }
163
164 @Override
165 public void onStart() {
166 mMountService = new MountService(getContext());
167 publishBinderService("mount", mMountService);
Jeff Sharkeycd575992016-03-29 14:12:49 -0600168 mMountService.start();
Jeff Sharkey56e62932015-03-21 20:41:00 -0700169 }
170
171 @Override
172 public void onBootPhase(int phase) {
173 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
174 mMountService.systemReady();
Yasuhiro Matsuda87a38b52015-07-24 22:10:16 +0900175 } else if (phase == SystemService.PHASE_BOOT_COMPLETED) {
176 mMountService.bootCompleted();
Jeff Sharkey56e62932015-03-21 20:41:00 -0700177 }
178 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700179
180 @Override
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700181 public void onUnlockUser(int userHandle) {
182 mMountService.onUnlockUser(userHandle);
Jeff Sharkey48877892015-03-18 11:27:19 -0700183 }
184
185 @Override
186 public void onCleanupUser(int userHandle) {
187 mMountService.onCleanupUser(userHandle);
188 }
Jeff Sharkey56e62932015-03-21 20:41:00 -0700189 }
190
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800191 private static final boolean DEBUG_EVENTS = false;
Kenny Rootb7db2722011-01-25 16:39:35 -0800192 private static final boolean DEBUG_OBB = false;
Kenny Root02c87302010-07-01 08:10:18 -0700193
Kenny Root07714d42011-08-17 17:49:28 -0700194 // Disable this since it messes up long-running cryptfs operations.
195 private static final boolean WATCHDOG_ENABLE = false;
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 private static final String TAG = "MountService";
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700198
Jeff Sharkey9756d752015-05-14 21:07:42 -0700199 private static final String TAG_STORAGE_BENCHMARK = "storage_benchmark";
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700200 private static final String TAG_STORAGE_TRIM = "storage_trim";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
Kenny Root305bcbf2010-09-03 07:56:38 -0700202 private static final String VOLD_TAG = "VoldConnector";
Paul Lawrence1c62cbb2015-06-03 14:14:52 -0700203 private static final String CRYPTD_TAG = "CryptdConnector";
Kenny Root305bcbf2010-09-03 07:56:38 -0700204
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700205 /** Maximum number of ASEC containers allowed to be mounted. */
206 private static final int MAX_CONTAINERS = 250;
207
Jeff Sharkey275e3e42015-04-24 16:10:32 -0700208 /** Magic value sent by MoveTask.cpp */
209 private static final int MOVE_STATUS_COPY_FINISHED = 82;
210
San Mehat4270e1e2010-01-29 05:32:19 -0800211 /*
212 * Internal vold response code constants
213 */
San Mehat22dd86e2010-01-12 12:21:18 -0800214 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -0800215 /*
216 * 100 series - Requestion action was initiated; expect another reply
217 * before proceeding with a new command.
218 */
San Mehat22dd86e2010-01-12 12:21:18 -0800219 public static final int VolumeListResult = 110;
220 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -0800221 public static final int StorageUsersListResult = 112;
Paul Lawrencee51dcf92014-03-18 10:56:00 -0700222 public static final int CryptfsGetfieldResult = 113;
San Mehat22dd86e2010-01-12 12:21:18 -0800223
San Mehat4270e1e2010-01-29 05:32:19 -0800224 /*
225 * 200 series - Requestion action has been successfully completed.
226 */
227 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800228 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800229 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800230
San Mehat4270e1e2010-01-29 05:32:19 -0800231 /*
232 * 400 series - Command was accepted, but the requested action
233 * did not take place.
234 */
235 public static final int OpFailedNoMedia = 401;
236 public static final int OpFailedMediaBlank = 402;
237 public static final int OpFailedMediaCorrupt = 403;
238 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800239 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700240 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800241
242 /*
243 * 600 series - Unsolicited broadcasts.
244 */
Jeff Sharkey48877892015-03-18 11:27:19 -0700245 public static final int DISK_CREATED = 640;
246 public static final int DISK_SIZE_CHANGED = 641;
247 public static final int DISK_LABEL_CHANGED = 642;
Jeff Sharkeyeba260d2015-04-19 14:35:16 -0700248 public static final int DISK_SCANNED = 643;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700249 public static final int DISK_SYS_PATH_CHANGED = 644;
Jeff Sharkey48877892015-03-18 11:27:19 -0700250 public static final int DISK_DESTROYED = 649;
251
252 public static final int VOLUME_CREATED = 650;
253 public static final int VOLUME_STATE_CHANGED = 651;
254 public static final int VOLUME_FS_TYPE_CHANGED = 652;
255 public static final int VOLUME_FS_UUID_CHANGED = 653;
256 public static final int VOLUME_FS_LABEL_CHANGED = 654;
257 public static final int VOLUME_PATH_CHANGED = 655;
Jeff Sharkey50a05452015-04-29 11:24:52 -0700258 public static final int VOLUME_INTERNAL_PATH_CHANGED = 656;
Jeff Sharkey48877892015-03-18 11:27:19 -0700259 public static final int VOLUME_DESTROYED = 659;
Svetoslavf23b64d2013-04-25 14:45:54 -0700260
Jeff Sharkey275e3e42015-04-24 16:10:32 -0700261 public static final int MOVE_STATUS = 660;
Jeff Sharkey9756d752015-05-14 21:07:42 -0700262 public static final int BENCHMARK_RESULT = 661;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700263 public static final int TRIM_RESULT = 662;
San Mehat22dd86e2010-01-12 12:21:18 -0800264 }
265
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700266 private static final int VERSION_INIT = 1;
267 private static final int VERSION_ADD_PRIMARY = 2;
Jeff Sharkeyfced5342015-05-10 14:53:34 -0700268 private static final int VERSION_FIX_PRIMARY = 3;
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700269
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700270 private static final String TAG_VOLUMES = "volumes";
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700271 private static final String ATTR_VERSION = "version";
272 private static final String ATTR_PRIMARY_STORAGE_UUID = "primaryStorageUuid";
Jeff Sharkey4c099d02015-05-15 13:45:00 -0700273 private static final String ATTR_FORCE_ADOPTABLE = "forceAdoptable";
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700274 private static final String TAG_VOLUME = "volume";
275 private static final String ATTR_TYPE = "type";
276 private static final String ATTR_FS_UUID = "fsUuid";
Jeff Sharkey5cc0df22015-06-17 19:44:05 -0700277 private static final String ATTR_PART_GUID = "partGuid";
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700278 private static final String ATTR_NICKNAME = "nickname";
279 private static final String ATTR_USER_FLAGS = "userFlags";
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700280 private static final String ATTR_CREATED_MILLIS = "createdMillis";
281 private static final String ATTR_LAST_TRIM_MILLIS = "lastTrimMillis";
282 private static final String ATTR_LAST_BENCH_MILLIS = "lastBenchMillis";
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700283
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700284 private final AtomicFile mSettingsFile;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700285
Jeff Sharkey48877892015-03-18 11:27:19 -0700286 /**
287 * <em>Never</em> hold the lock while performing downcalls into vold, since
288 * unsolicited events can suddenly appear to update data structures.
289 */
290 private final Object mLock = new Object();
291
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700292 /** Set of users that we know are unlocked. */
Jeff Sharkey48877892015-03-18 11:27:19 -0700293 @GuardedBy("mLock")
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700294 private int[] mLocalUnlockedUsers = EmptyArray.INT;
295 /** Set of users that system knows are unlocked. */
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -0800296 @GuardedBy("mLock")
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700297 private int[] mSystemUnlockedUsers = EmptyArray.INT;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700298
299 /** Map from disk ID to disk */
Jeff Sharkey48877892015-03-18 11:27:19 -0700300 @GuardedBy("mLock")
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700301 private ArrayMap<String, DiskInfo> mDisks = new ArrayMap<>();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700302 /** Map from volume ID to disk */
Jeff Sharkey48877892015-03-18 11:27:19 -0700303 @GuardedBy("mLock")
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700304 private final ArrayMap<String, VolumeInfo> mVolumes = new ArrayMap<>();
Jeff Sharkey48877892015-03-18 11:27:19 -0700305
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700306 /** Map from UUID to record */
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700307 @GuardedBy("mLock")
Jeff Sharkeyb36586a2015-04-27 08:42:28 -0700308 private ArrayMap<String, VolumeRecord> mRecords = new ArrayMap<>();
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700309 @GuardedBy("mLock")
310 private String mPrimaryStorageUuid;
Jeff Sharkey4c099d02015-05-15 13:45:00 -0700311 @GuardedBy("mLock")
312 private boolean mForceAdoptable;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700313
Jeff Sharkeyeba260d2015-04-19 14:35:16 -0700314 /** Map from disk ID to latches */
315 @GuardedBy("mLock")
316 private ArrayMap<String, CountDownLatch> mDiskScanLatches = new ArrayMap<>();
317
Jeff Sharkey275e3e42015-04-24 16:10:32 -0700318 @GuardedBy("mLock")
319 private IPackageMoveObserver mMoveCallback;
320 @GuardedBy("mLock")
321 private String mMoveTargetUuid;
322
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700323 private VolumeInfo findVolumeByIdOrThrow(String id) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700324 synchronized (mLock) {
325 final VolumeInfo vol = mVolumes.get(id);
326 if (vol != null) {
327 return vol;
328 }
329 }
330 throw new IllegalArgumentException("No volume found for ID " + id);
331 }
332
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700333 private String findVolumeIdForPathOrThrow(String path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700334 synchronized (mLock) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700335 for (int i = 0; i < mVolumes.size(); i++) {
336 final VolumeInfo vol = mVolumes.valueAt(i);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700337 if (vol.path != null && path.startsWith(vol.path)) {
338 return vol.id;
Jeff Sharkey48877892015-03-18 11:27:19 -0700339 }
340 }
341 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700342 throw new IllegalArgumentException("No volume found for path " + path);
Jeff Sharkey48877892015-03-18 11:27:19 -0700343 }
344
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700345 private VolumeRecord findRecordForPath(String path) {
346 synchronized (mLock) {
347 for (int i = 0; i < mVolumes.size(); i++) {
348 final VolumeInfo vol = mVolumes.valueAt(i);
349 if (vol.path != null && path.startsWith(vol.path)) {
350 return mRecords.get(vol.fsUuid);
351 }
352 }
353 }
354 return null;
355 }
356
357 private String scrubPath(String path) {
358 if (path.startsWith(Environment.getDataDirectory().getAbsolutePath())) {
359 return "internal";
360 }
361 final VolumeRecord rec = findRecordForPath(path);
362 if (rec == null || rec.createdMillis == 0) {
363 return "unknown";
364 } else {
365 return "ext:" + (int) ((System.currentTimeMillis() - rec.createdMillis)
366 / DateUtils.WEEK_IN_MILLIS) + "w";
367 }
368 }
369
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700370 private @Nullable VolumeInfo findStorageForUuid(String volumeUuid) {
Jeff Sharkey275e3e42015-04-24 16:10:32 -0700371 final StorageManager storage = mContext.getSystemService(StorageManager.class);
372 if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) {
Jeff Sharkeyef10ee02015-07-05 14:17:27 -0700373 return storage.findVolumeById(VolumeInfo.ID_EMULATED_INTERNAL);
Jeff Sharkey275e3e42015-04-24 16:10:32 -0700374 } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) {
375 return storage.getPrimaryPhysicalVolume();
376 } else {
377 return storage.findEmulatedForPrivate(storage.findVolumeByUuid(volumeUuid));
378 }
379 }
380
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700381 private boolean shouldBenchmark() {
382 final long benchInterval = Settings.Global.getLong(mContext.getContentResolver(),
383 Settings.Global.STORAGE_BENCHMARK_INTERVAL, DateUtils.WEEK_IN_MILLIS);
Jeff Sharkeye83d8a92015-09-09 14:53:38 -0700384 if (benchInterval == -1) {
385 return false;
386 } else if (benchInterval == 0) {
387 return true;
388 }
389
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700390 synchronized (mLock) {
391 for (int i = 0; i < mVolumes.size(); i++) {
392 final VolumeInfo vol = mVolumes.valueAt(i);
393 final VolumeRecord rec = mRecords.get(vol.fsUuid);
Jeff Sharkeye83d8a92015-09-09 14:53:38 -0700394 if (vol.isMountedWritable() && rec != null) {
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700395 final long benchAge = System.currentTimeMillis() - rec.lastBenchMillis;
396 if (benchAge >= benchInterval) {
397 return true;
398 }
399 }
400 }
401 return false;
402 }
403 }
404
Jeff Sharkeyeba260d2015-04-19 14:35:16 -0700405 private CountDownLatch findOrCreateDiskScanLatch(String diskId) {
406 synchronized (mLock) {
407 CountDownLatch latch = mDiskScanLatches.get(diskId);
408 if (latch == null) {
409 latch = new CountDownLatch(1);
410 mDiskScanLatches.put(diskId, latch);
411 }
412 return latch;
413 }
414 }
415
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -0800416 private static String escapeNull(String arg) {
417 if (TextUtils.isEmpty(arg)) {
418 return "!";
419 } else {
420 if (arg.indexOf('\0') != -1 || arg.indexOf(' ') != -1) {
421 throw new IllegalArgumentException(arg);
422 }
423 return arg;
424 }
425 }
426
Paul Lawrence8e397362014-01-27 15:22:30 -0800427 /** List of crypto types.
428 * These must match CRYPT_TYPE_XXX in cryptfs.h AND their
429 * corresponding commands in CommandListener.cpp */
430 public static final String[] CRYPTO_TYPES
431 = { "password", "default", "pattern", "pin" };
432
Brian Carlstrom7395a8a2014-04-28 22:11:01 -0700433 private final Context mContext;
Jeff Sharkeycd575992016-03-29 14:12:49 -0600434
Brian Carlstromdfad99a2014-05-07 15:21:14 -0700435 private final NativeDaemonConnector mConnector;
Paul Lawrence1c62cbb2015-06-03 14:14:52 -0700436 private final NativeDaemonConnector mCryptConnector;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700437
Jeff Sharkeycd575992016-03-29 14:12:49 -0600438 private final Thread mConnectorThread;
439 private final Thread mCryptConnectorThread;
440
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700441 private volatile boolean mSystemReady = false;
Yasuhiro Matsuda87a38b52015-07-24 22:10:16 +0900442 private volatile boolean mBootCompleted = false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700443 private volatile boolean mDaemonConnected = false;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700444
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700445 private PackageManagerService mPms;
446
447 private final Callbacks mCallbacks;
Jeff Sharkeyb9fe5372015-12-03 15:23:08 -0700448 private final LockPatternUtils mLockPatternUtils;
Jeff Sharkey48877892015-03-18 11:27:19 -0700449
Paul Lawrence1c62cbb2015-06-03 14:14:52 -0700450 // Two connectors - mConnector & mCryptConnector
451 private final CountDownLatch mConnectedSignal = new CountDownLatch(2);
Jeff Sharkey0be607c2012-11-14 14:39:19 -0800452 private final CountDownLatch mAsecsScanned = new CountDownLatch(1);
Jeff Sharkey48877892015-03-18 11:27:19 -0700453
454 private final Object mUnmountLock = new Object();
455 @GuardedBy("mUnmountLock")
456 private CountDownLatch mUnmountSignal;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800457
San Mehat6cdd9c02010-02-09 14:45:20 -0800458 /**
459 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800460 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800461 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800462 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800463
Kenny Root02c87302010-07-01 08:10:18 -0700464 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700465 * The size of the crypto algorithm key in bits for OBB files. Currently
466 * Twofish is used which takes 128-bit keys.
467 */
468 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
469
470 /**
471 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
472 * 1024 is reasonably secure and not too slow.
473 */
474 private static final int PBKDF2_HASH_ROUNDS = 1024;
475
476 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700477 * Mounted OBB tracking information. Used to track the current state of all
478 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700479 */
Kenny Root735de3b2010-09-30 14:11:39 -0700480 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700481
482 /** Map from raw paths to {@link ObbState}. */
Kenny Roota02b8b02010-08-05 16:14:17 -0700483 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
484
Svet Ganov6ee871e2015-07-10 14:29:33 -0700485 // Not guarded by a lock.
486 private final MountServiceInternalImpl mMountServiceInternal = new MountServiceInternalImpl();
487
Kenny Roota02b8b02010-08-05 16:14:17 -0700488 class ObbState implements IBinder.DeathRecipient {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700489 public ObbState(String rawPath, String canonicalPath, int callingUid,
490 IObbActionListener token, int nonce) {
491 this.rawPath = rawPath;
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700492 this.canonicalPath = canonicalPath;
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700493
494 this.ownerGid = UserHandle.getSharedAppGid(callingUid);
Kenny Rootaf9d6672010-10-08 09:21:39 -0700495 this.token = token;
496 this.nonce = nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700497 }
498
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700499 final String rawPath;
500 final String canonicalPath;
Kenny Roota02b8b02010-08-05 16:14:17 -0700501
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700502 final int ownerGid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700503
Kenny Rootaf9d6672010-10-08 09:21:39 -0700504 // Token of remote Binder caller
505 final IObbActionListener token;
506
507 // Identifier to pass back to the token
508 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700509
Kenny Root735de3b2010-09-30 14:11:39 -0700510 public IBinder getBinder() {
511 return token.asBinder();
512 }
513
Kenny Roota02b8b02010-08-05 16:14:17 -0700514 @Override
515 public void binderDied() {
516 ObbAction action = new UnmountObbAction(this, true);
517 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700518 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700519
Kenny Root5919ac62010-10-05 09:49:40 -0700520 public void link() throws RemoteException {
521 getBinder().linkToDeath(this, 0);
522 }
523
524 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700525 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700526 }
Kenny Root38cf8862010-09-26 14:18:51 -0700527
528 @Override
529 public String toString() {
530 StringBuilder sb = new StringBuilder("ObbState{");
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700531 sb.append("rawPath=").append(rawPath);
532 sb.append(",canonicalPath=").append(canonicalPath);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700533 sb.append(",ownerGid=").append(ownerGid);
534 sb.append(",token=").append(token);
535 sb.append(",binder=").append(getBinder());
Kenny Root38cf8862010-09-26 14:18:51 -0700536 sb.append('}');
537 return sb.toString();
538 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700539 }
540
541 // OBB Action Handler
542 final private ObbActionHandler mObbActionHandler;
543
544 // OBB action handler messages
545 private static final int OBB_RUN_ACTION = 1;
546 private static final int OBB_MCS_BOUND = 2;
547 private static final int OBB_MCS_UNBIND = 3;
548 private static final int OBB_MCS_RECONNECT = 4;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700549 private static final int OBB_FLUSH_MOUNT_STATE = 5;
Kenny Roota02b8b02010-08-05 16:14:17 -0700550
551 /*
552 * Default Container Service information
553 */
554 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
555 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
556
557 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
558
559 class DefaultContainerConnection implements ServiceConnection {
Jeff Sharkey48877892015-03-18 11:27:19 -0700560 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -0700561 public void onServiceConnected(ComponentName name, IBinder service) {
562 if (DEBUG_OBB)
563 Slog.i(TAG, "onServiceConnected");
564 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
565 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
566 }
567
Jeff Sharkey48877892015-03-18 11:27:19 -0700568 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -0700569 public void onServiceDisconnected(ComponentName name) {
570 if (DEBUG_OBB)
571 Slog.i(TAG, "onServiceDisconnected");
572 }
573 };
574
575 // Used in the ObbActionHandler
576 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700577
Christopher Tate7265abe2014-11-21 13:54:45 -0800578 // Last fstrim operation tracking
579 private static final String LAST_FSTRIM_FILE = "last-fstrim";
580 private final File mLastMaintenanceFile;
581 private long mLastMaintenance;
582
Kenny Root02c87302010-07-01 08:10:18 -0700583 // Handler messages
Jeff Sharkey48877892015-03-18 11:27:19 -0700584 private static final int H_SYSTEM_READY = 1;
585 private static final int H_DAEMON_CONNECTED = 2;
586 private static final int H_SHUTDOWN = 3;
587 private static final int H_FSTRIM = 4;
588 private static final int H_VOLUME_MOUNT = 5;
589 private static final int H_VOLUME_BROADCAST = 6;
Jeff Sharkeyabc3e852015-08-03 14:41:13 -0700590 private static final int H_INTERNAL_BROADCAST = 7;
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700591 private static final int H_VOLUME_UNMOUNT = 8;
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800592 private static final int H_PARTITION_FORGET = 9;
593 private static final int H_RESET = 10;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800594
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400595 class MountServiceHandler extends Handler {
Jeff Sharkey48877892015-03-18 11:27:19 -0700596 public MountServiceHandler(Looper looper) {
597 super(looper);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400598 }
599
Jason parks5af0b912010-11-29 09:05:25 -0600600 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800601 public void handleMessage(Message msg) {
602 switch (msg.what) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700603 case H_SYSTEM_READY: {
Jeff Sharkey48877892015-03-18 11:27:19 -0700604 handleSystemReady();
605 break;
606 }
607 case H_DAEMON_CONNECTED: {
608 handleDaemonConnected();
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700609 break;
610 }
Christopher Tated417d622013-08-19 16:14:25 -0700611 case H_FSTRIM: {
Jeff Sharkey1783f142015-04-17 10:52:51 -0700612 if (!isReady()) {
613 Slog.i(TAG, "fstrim requested, but no daemon connection yet; trying again");
Christopher Tate7618db12015-04-28 16:32:55 -0700614 sendMessageDelayed(obtainMessage(H_FSTRIM, msg.obj),
615 DateUtils.SECOND_IN_MILLIS);
616 break;
Jeff Sharkey1783f142015-04-17 10:52:51 -0700617 }
618
Christopher Tated417d622013-08-19 16:14:25 -0700619 Slog.i(TAG, "Running fstrim idle maintenance");
Christopher Tate7265abe2014-11-21 13:54:45 -0800620
621 // Remember when we kicked it off
622 try {
623 mLastMaintenance = System.currentTimeMillis();
624 mLastMaintenanceFile.setLastModified(mLastMaintenance);
625 } catch (Exception e) {
626 Slog.e(TAG, "Unable to record last fstrim!");
627 }
628
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700629 final boolean shouldBenchmark = shouldBenchmark();
Christopher Tated417d622013-08-19 16:14:25 -0700630 try {
631 // This method must be run on the main (handler) thread,
632 // so it is safe to directly call into vold.
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700633 mConnector.execute("fstrim", shouldBenchmark ? "dotrimbench" : "dotrim");
Christopher Tated417d622013-08-19 16:14:25 -0700634 } catch (NativeDaemonConnectorException ndce) {
635 Slog.e(TAG, "Failed to run fstrim!");
636 }
Christopher Tate7265abe2014-11-21 13:54:45 -0800637
Christopher Tated417d622013-08-19 16:14:25 -0700638 // invoke the completion callback, if any
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700639 // TODO: fstrim is non-blocking, so remove this useless callback
Christopher Tated417d622013-08-19 16:14:25 -0700640 Runnable callback = (Runnable) msg.obj;
641 if (callback != null) {
642 callback.run();
643 }
644 break;
645 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700646 case H_SHUTDOWN: {
647 final IMountShutdownObserver obs = (IMountShutdownObserver) msg.obj;
648 boolean success = false;
649 try {
650 success = mConnector.execute("volume", "shutdown").isClassOk();
651 } catch (NativeDaemonConnectorException ignored) {
652 }
653 if (obs != null) {
654 try {
655 obs.onShutDownComplete(success ? 0 : -1);
656 } catch (RemoteException ignored) {
657 }
658 }
659 break;
660 }
661 case H_VOLUME_MOUNT: {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700662 final VolumeInfo vol = (VolumeInfo) msg.obj;
Jeff Sharkey2e606d72015-07-27 14:19:54 -0700663 if (isMountDisallowed(vol)) {
664 Slog.i(TAG, "Ignoring mount " + vol.getId() + " due to policy");
665 break;
666 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700667 try {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700668 mConnector.execute("volume", "mount", vol.id, vol.mountFlags,
669 vol.mountUserId);
Jeff Sharkey48877892015-03-18 11:27:19 -0700670 } catch (NativeDaemonConnectorException ignored) {
671 }
672 break;
673 }
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700674 case H_VOLUME_UNMOUNT: {
675 final VolumeInfo vol = (VolumeInfo) msg.obj;
676 unmount(vol.getId());
677 break;
678 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700679 case H_VOLUME_BROADCAST: {
680 final StorageVolume userVol = (StorageVolume) msg.obj;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700681 final String envState = userVol.getState();
682 Slog.d(TAG, "Volume " + userVol.getId() + " broadcasting " + envState + " to "
Jeff Sharkey48877892015-03-18 11:27:19 -0700683 + userVol.getOwner());
684
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700685 final String action = VolumeInfo.getBroadcastForEnvironment(envState);
Jeff Sharkey48877892015-03-18 11:27:19 -0700686 if (action != null) {
687 final Intent intent = new Intent(action,
688 Uri.fromFile(userVol.getPathFile()));
689 intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, userVol);
690 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
691 mContext.sendBroadcastAsUser(intent, userVol.getOwner());
692 }
693 break;
694 }
Jeff Sharkeyabc3e852015-08-03 14:41:13 -0700695 case H_INTERNAL_BROADCAST: {
696 // Internal broadcasts aimed at system components, not for
697 // third-party apps.
698 final Intent intent = (Intent) msg.obj;
699 mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
700 android.Manifest.permission.WRITE_MEDIA_STORAGE);
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800701 break;
702 }
703 case H_PARTITION_FORGET: {
704 final String partGuid = (String) msg.obj;
705 forgetPartition(partGuid);
706 break;
707 }
708 case H_RESET: {
709 resetIfReadyAndConnected();
710 break;
Jeff Sharkeyabc3e852015-08-03 14:41:13 -0700711 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800712 }
713 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700714 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700715
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700716 private final Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800717
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700718 private BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
719 @Override
720 public void onReceive(Context context, Intent intent) {
721 final String action = intent.getAction();
722 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700723 Preconditions.checkArgument(userId >= 0);
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700724
725 try {
726 if (Intent.ACTION_USER_ADDED.equals(action)) {
727 final UserManager um = mContext.getSystemService(UserManager.class);
728 final int userSerialNumber = um.getUserSerialNumber(userId);
729 mConnector.execute("volume", "user_added", userId, userSerialNumber);
730 } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700731 synchronized (mVolumes) {
732 final int size = mVolumes.size();
733 for (int i = 0; i < size; i++) {
734 final VolumeInfo vol = mVolumes.valueAt(i);
735 if (vol.mountUserId == userId) {
736 vol.mountUserId = UserHandle.USER_NULL;
737 mHandler.obtainMessage(H_VOLUME_UNMOUNT, vol).sendToTarget();
738 }
739 }
740 }
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700741 mConnector.execute("volume", "user_removed", userId);
742 }
743 } catch (NativeDaemonConnectorException e) {
744 Slog.w(TAG, "Failed to send user details to vold", e);
745 }
746 }
747 };
748
Jeff Sharkey56e62932015-03-21 20:41:00 -0700749 @Override
750 public void waitForAsecScan() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700751 waitForLatch(mAsecsScanned, "mAsecsScanned");
Kenny Root51a573c2012-05-17 13:30:28 -0700752 }
753
San Mehat207e5382010-02-04 20:46:54 -0800754 private void waitForReady() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700755 waitForLatch(mConnectedSignal, "mConnectedSignal");
Kenny Root51a573c2012-05-17 13:30:28 -0700756 }
757
Jeff Sharkey48877892015-03-18 11:27:19 -0700758 private void waitForLatch(CountDownLatch latch, String condition) {
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -0700759 try {
760 waitForLatch(latch, condition, -1);
761 } catch (TimeoutException ignored) {
762 }
763 }
764
765 private void waitForLatch(CountDownLatch latch, String condition, long timeoutMillis)
766 throws TimeoutException {
767 final long startMillis = SystemClock.elapsedRealtime();
Jeff Sharkeyeba260d2015-04-19 14:35:16 -0700768 while (true) {
Kenny Root51a573c2012-05-17 13:30:28 -0700769 try {
770 if (latch.await(5000, TimeUnit.MILLISECONDS)) {
San Mehat207e5382010-02-04 20:46:54 -0800771 return;
Kenny Root51a573c2012-05-17 13:30:28 -0700772 } else {
773 Slog.w(TAG, "Thread " + Thread.currentThread().getName()
Jeff Sharkey48877892015-03-18 11:27:19 -0700774 + " still waiting for " + condition + "...");
San Mehat207e5382010-02-04 20:46:54 -0800775 }
Kenny Root51a573c2012-05-17 13:30:28 -0700776 } catch (InterruptedException e) {
Jeff Sharkeyeba260d2015-04-19 14:35:16 -0700777 Slog.w(TAG, "Interrupt while waiting for " + condition);
San Mehat207e5382010-02-04 20:46:54 -0800778 }
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -0700779 if (timeoutMillis > 0 && SystemClock.elapsedRealtime() > startMillis + timeoutMillis) {
780 throw new TimeoutException("Thread " + Thread.currentThread().getName()
781 + " gave up waiting for " + condition + " after " + timeoutMillis + "ms");
782 }
San Mehat207e5382010-02-04 20:46:54 -0800783 }
San Mehat1f6301e2010-01-07 22:40:27 -0800784 }
Kenny Root02c87302010-07-01 08:10:18 -0700785
Paul Lawrence945490c2014-03-27 16:37:28 +0000786 private boolean isReady() {
787 try {
788 return mConnectedSignal.await(0, TimeUnit.MILLISECONDS);
789 } catch (InterruptedException e) {
790 return false;
791 }
792 }
793
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700794 private void handleSystemReady() {
Jeff Sharkey8924e872015-11-30 12:52:10 -0700795 initIfReadyAndConnected();
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800796 resetIfReadyAndConnected();
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700797
Jeff Sharkey48877892015-03-18 11:27:19 -0700798 // Start scheduling nominally-daily fstrim operations
Christopher Tate115afda2014-06-06 19:06:26 -0700799 MountServiceIdler.scheduleIdlePass(mContext);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700800 }
801
Jeff Sharkey14cbe522015-07-08 14:06:37 -0700802 /**
803 * MediaProvider has a ton of code that makes assumptions about storage
804 * paths never changing, so we outright kill them to pick up new state.
805 */
806 @Deprecated
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700807 private void killMediaProvider(List<UserInfo> users) {
808 if (users == null) return;
809
Jeff Sharkeyb3cf9532015-07-17 15:12:39 -0700810 final long token = Binder.clearCallingIdentity();
811 try {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700812 for (UserInfo user : users) {
813 // System user does not have media provider, so skip.
814 if (user.isSystemOnly()) continue;
815
Jeff Sharkey2a9e3f82015-12-18 10:57:58 -0700816 final ProviderInfo provider = mPms.resolveContentProvider(MediaStore.AUTHORITY,
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600817 PackageManager.MATCH_DIRECT_BOOT_AWARE
818 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
819 user.id);
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700820 if (provider != null) {
821 final IActivityManager am = ActivityManagerNative.getDefault();
822 try {
823 am.killApplicationWithAppId(provider.applicationInfo.packageName,
824 UserHandle.getAppId(provider.applicationInfo.uid), "vold reset");
825 // We only need to run this once. It will kill all users' media processes.
826 break;
827 } catch (RemoteException e) {
828 }
Jeff Sharkeyb3cf9532015-07-17 15:12:39 -0700829 }
Jeff Sharkey14cbe522015-07-08 14:06:37 -0700830 }
Jeff Sharkeyb3cf9532015-07-17 15:12:39 -0700831 } finally {
832 Binder.restoreCallingIdentity(token);
Jeff Sharkey14cbe522015-07-08 14:06:37 -0700833 }
834 }
835
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800836 private void addInternalVolumeLocked() {
Amith Yamasania7892482015-08-07 11:09:05 -0700837 // Create a stub volume that represents internal storage
838 final VolumeInfo internal = new VolumeInfo(VolumeInfo.ID_PRIVATE_INTERNAL,
839 VolumeInfo.TYPE_PRIVATE, null, null);
840 internal.state = VolumeInfo.STATE_MOUNTED;
841 internal.path = Environment.getDataDirectory().getAbsolutePath();
842 mVolumes.put(internal.id, internal);
843 }
844
Jeff Sharkey8924e872015-11-30 12:52:10 -0700845 private void initIfReadyAndConnected() {
846 Slog.d(TAG, "Thinking about init, mSystemReady=" + mSystemReady
847 + ", mDaemonConnected=" + mDaemonConnected);
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700848 if (mSystemReady && mDaemonConnected
Paul Lawrence20be5d62016-02-26 13:51:17 -0800849 && !StorageManager.isFileEncryptedNativeOnly()) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700850 // When booting a device without native support, make sure that our
851 // user directories are locked or unlocked based on the current
852 // emulation status.
Paul Lawrence20be5d62016-02-26 13:51:17 -0800853 final boolean initLocked = StorageManager.isFileEncryptedEmulatedOnly();
Paul Crowleyd94ab732016-02-15 06:44:51 +0000854 Slog.d(TAG, "Setting up emulation state, initlocked=" + initLocked);
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700855 final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers();
Jeff Sharkey8924e872015-11-30 12:52:10 -0700856 for (UserInfo user : users) {
857 try {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700858 if (initLocked) {
859 mCryptConnector.execute("cryptfs", "lock_user_key", user.id);
860 } else {
861 mCryptConnector.execute("cryptfs", "unlock_user_key", user.id,
Paul Crowleyd94ab732016-02-15 06:44:51 +0000862 user.serialNumber, "!", "!");
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700863 }
Jeff Sharkey8924e872015-11-30 12:52:10 -0700864 } catch (NativeDaemonConnectorException e) {
865 Slog.w(TAG, "Failed to init vold", e);
866 }
867 }
868 }
869 }
870
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800871 private void resetIfReadyAndConnected() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700872 Slog.d(TAG, "Thinking about reset, mSystemReady=" + mSystemReady
873 + ", mDaemonConnected=" + mDaemonConnected);
874 if (mSystemReady && mDaemonConnected) {
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800875 final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers();
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700876 killMediaProvider(users);
Jeff Sharkey14cbe522015-07-08 14:06:37 -0700877
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700878 final int[] systemUnlockedUsers;
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800879 synchronized (mLock) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700880 systemUnlockedUsers = mSystemUnlockedUsers;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700881
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800882 mDisks.clear();
883 mVolumes.clear();
884
885 addInternalVolumeLocked();
886 }
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700887
Jeff Sharkey48877892015-03-18 11:27:19 -0700888 try {
889 mConnector.execute("volume", "reset");
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700890
891 // Tell vold about all existing and started users
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700892 for (UserInfo user : users) {
893 mConnector.execute("volume", "user_added", user.id, user.serialNumber);
894 }
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700895 for (int userId : systemUnlockedUsers) {
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700896 mConnector.execute("volume", "user_started", userId);
Jeff Sharkey50a05452015-04-29 11:24:52 -0700897 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700898 } catch (NativeDaemonConnectorException e) {
899 Slog.w(TAG, "Failed to reset vold", e);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700900 }
901 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700902 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700903
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700904 private void onUnlockUser(int userId) {
905 Slog.d(TAG, "onUnlockUser " + userId);
Jeff Sharkey48877892015-03-18 11:27:19 -0700906
907 // We purposefully block here to make sure that user-specific
908 // staging area is ready so it's ready for zygote-forked apps to
909 // bind mount against.
910 try {
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700911 mConnector.execute("volume", "user_started", userId);
Jeff Sharkey48877892015-03-18 11:27:19 -0700912 } catch (NativeDaemonConnectorException ignored) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700913 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700914
915 // Record user as started so newly mounted volumes kick off events
916 // correctly, then synthesize events for any already-mounted volumes.
917 synchronized (mVolumes) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700918 for (int i = 0; i < mVolumes.size(); i++) {
919 final VolumeInfo vol = mVolumes.valueAt(i);
Jeff Sharkey46349872015-07-28 10:49:47 -0700920 if (vol.isVisibleForRead(userId) && vol.isMountedReadable()) {
Svet Ganov6ee871e2015-07-10 14:29:33 -0700921 final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, false);
Jeff Sharkey48877892015-03-18 11:27:19 -0700922 mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget();
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700923
Jeff Sharkey27de30d2015-04-18 16:20:27 -0700924 final String envState = VolumeInfo.getEnvironmentForState(vol.getState());
925 mCallbacks.notifyStorageStateChanged(userVol.getPath(), envState, envState);
Jeff Sharkey48877892015-03-18 11:27:19 -0700926 }
927 }
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700928 mSystemUnlockedUsers = ArrayUtils.appendInt(mSystemUnlockedUsers, userId);
Jeff Sharkey48877892015-03-18 11:27:19 -0700929 }
930 }
931
932 private void onCleanupUser(int userId) {
933 Slog.d(TAG, "onCleanupUser " + userId);
934
935 try {
Jeff Sharkeybcd262d2015-06-10 09:41:17 -0700936 mConnector.execute("volume", "user_stopped", userId);
Jeff Sharkey48877892015-03-18 11:27:19 -0700937 } catch (NativeDaemonConnectorException ignored) {
938 }
939
940 synchronized (mVolumes) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -0700941 mSystemUnlockedUsers = ArrayUtils.removeInt(mSystemUnlockedUsers, userId);
Jeff Sharkey48877892015-03-18 11:27:19 -0700942 }
943 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700944
Christopher Tated417d622013-08-19 16:14:25 -0700945 void runIdleMaintenance(Runnable callback) {
946 mHandler.sendMessage(mHandler.obtainMessage(H_FSTRIM, callback));
947 }
948
Christopher Tate7265abe2014-11-21 13:54:45 -0800949 // Binder entry point for kicking off an immediate fstrim
950 @Override
951 public void runMaintenance() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700952 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
Christopher Tate7265abe2014-11-21 13:54:45 -0800953 runIdleMaintenance(null);
954 }
955
956 @Override
957 public long lastMaintenance() {
958 return mLastMaintenance;
959 }
960
San Mehat4270e1e2010-01-29 05:32:19 -0800961 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800962 * Callback from NativeDaemonConnector
963 */
Jeff Sharkey48877892015-03-18 11:27:19 -0700964 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800965 public void onDaemonConnected() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700966 mDaemonConnected = true;
967 mHandler.obtainMessage(H_DAEMON_CONNECTED).sendToTarget();
968 }
969
970 private void handleDaemonConnected() {
Jeff Sharkey8924e872015-11-30 12:52:10 -0700971 initIfReadyAndConnected();
Jeff Sharkey5a9bb742015-11-03 10:15:57 -0800972 resetIfReadyAndConnected();
Jeff Sharkey48877892015-03-18 11:27:19 -0700973
San Mehat4270e1e2010-01-29 05:32:19 -0800974 /*
Jeff Sharkey48877892015-03-18 11:27:19 -0700975 * Now that we've done our initialization, release
976 * the hounds!
San Mehat4270e1e2010-01-29 05:32:19 -0800977 */
Jeff Sharkey48877892015-03-18 11:27:19 -0700978 mConnectedSignal.countDown();
Paul Lawrence1c62cbb2015-06-03 14:14:52 -0700979 if (mConnectedSignal.getCount() != 0) {
980 // More daemons need to connect
981 return;
982 }
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400983
Jeff Sharkey48877892015-03-18 11:27:19 -0700984 // On an encrypted device we can't see system properties yet, so pull
985 // the system locale out of the mount service.
986 if ("".equals(SystemProperties.get("vold.encrypt_progress"))) {
987 copyLocaleFromMountService();
988 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700989
Jeff Sharkey48877892015-03-18 11:27:19 -0700990 // Let package manager load internal ASECs.
991 mPms.scanAvailableAsecs();
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400992
Jeff Sharkey48877892015-03-18 11:27:19 -0700993 // Notify people waiting for ASECs to be scanned that it's done.
994 mAsecsScanned.countDown();
San Mehat4270e1e2010-01-29 05:32:19 -0800995 }
996
Elliott Hughesf839b4f2014-09-26 12:30:47 -0700997 private void copyLocaleFromMountService() {
998 String systemLocale;
999 try {
1000 systemLocale = getField(StorageManager.SYSTEM_LOCALE_KEY);
1001 } catch (RemoteException e) {
1002 return;
1003 }
1004 if (TextUtils.isEmpty(systemLocale)) {
1005 return;
1006 }
1007
1008 Slog.d(TAG, "Got locale " + systemLocale + " from mount service");
1009 Locale locale = Locale.forLanguageTag(systemLocale);
1010 Configuration config = new Configuration();
1011 config.setLocale(locale);
1012 try {
1013 ActivityManagerNative.getDefault().updateConfiguration(config);
1014 } catch (RemoteException e) {
1015 Slog.e(TAG, "Error setting system locale from mount service", e);
1016 }
Elliott Hughes9c33f282014-10-13 12:39:56 -07001017
1018 // Temporary workaround for http://b/17945169.
1019 Slog.d(TAG, "Setting system properties to " + systemLocale + " from mount service");
Narayan Kamathd30dbb82015-01-15 14:48:15 +00001020 SystemProperties.set("persist.sys.locale", locale.toLanguageTag());
Elliott Hughesf839b4f2014-09-26 12:30:47 -07001021 }
1022
San Mehat4270e1e2010-01-29 05:32:19 -08001023 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001024 * Callback from NativeDaemonConnector
1025 */
Jeff Sharkey48877892015-03-18 11:27:19 -07001026 @Override
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001027 public boolean onCheckHoldWakeLock(int code) {
1028 return false;
1029 }
1030
1031 /**
1032 * Callback from NativeDaemonConnector
1033 */
Jeff Sharkey48877892015-03-18 11:27:19 -07001034 @Override
San Mehat4270e1e2010-01-29 05:32:19 -08001035 public boolean onEvent(int code, String raw, String[] cooked) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001036 synchronized (mLock) {
1037 return onEventLocked(code, raw, cooked);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001038 }
Jeff Sharkey48877892015-03-18 11:27:19 -07001039 }
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07001040
Jeff Sharkey48877892015-03-18 11:27:19 -07001041 private boolean onEventLocked(int code, String raw, String[] cooked) {
1042 switch (code) {
1043 case VoldResponseCode.DISK_CREATED: {
1044 if (cooked.length != 3) break;
1045 final String id = cooked[1];
Jeff Sharkey74acbbb2015-04-21 12:14:03 -07001046 int flags = Integer.parseInt(cooked[2]);
Jeff Sharkey4c099d02015-05-15 13:45:00 -07001047 if (SystemProperties.getBoolean(StorageManager.PROP_FORCE_ADOPTABLE, false)
1048 || mForceAdoptable) {
Jeff Sharkey74acbbb2015-04-21 12:14:03 -07001049 flags |= DiskInfo.FLAG_ADOPTABLE;
1050 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001051 mDisks.put(id, new DiskInfo(id, flags));
Jeff Sharkey48877892015-03-18 11:27:19 -07001052 break;
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07001053 }
Jeff Sharkey48877892015-03-18 11:27:19 -07001054 case VoldResponseCode.DISK_SIZE_CHANGED: {
1055 if (cooked.length != 3) break;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001056 final DiskInfo disk = mDisks.get(cooked[1]);
Jeff Sharkey48877892015-03-18 11:27:19 -07001057 if (disk != null) {
1058 disk.size = Long.parseLong(cooked[2]);
San Mehat4270e1e2010-01-29 05:32:19 -08001059 }
Jeff Sharkey48877892015-03-18 11:27:19 -07001060 break;
1061 }
1062 case VoldResponseCode.DISK_LABEL_CHANGED: {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001063 final DiskInfo disk = mDisks.get(cooked[1]);
Jeff Sharkey48877892015-03-18 11:27:19 -07001064 if (disk != null) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -07001065 final StringBuilder builder = new StringBuilder();
1066 for (int i = 2; i < cooked.length; i++) {
1067 builder.append(cooked[i]).append(' ');
1068 }
1069 disk.label = builder.toString().trim();
Jeff Sharkey48877892015-03-18 11:27:19 -07001070 }
1071 break;
1072 }
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001073 case VoldResponseCode.DISK_SCANNED: {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07001074 if (cooked.length != 2) break;
1075 final DiskInfo disk = mDisks.get(cooked[1]);
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001076 if (disk != null) {
1077 onDiskScannedLocked(disk);
1078 }
Jeff Sharkey59d577a2015-04-11 21:27:21 -07001079 break;
1080 }
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07001081 case VoldResponseCode.DISK_SYS_PATH_CHANGED: {
1082 if (cooked.length != 3) break;
1083 final DiskInfo disk = mDisks.get(cooked[1]);
1084 if (disk != null) {
1085 disk.sysPath = cooked[2];
1086 }
1087 break;
1088 }
Jeff Sharkey48877892015-03-18 11:27:19 -07001089 case VoldResponseCode.DISK_DESTROYED: {
1090 if (cooked.length != 2) break;
Makoto Onuki9dc575d2015-06-12 16:10:25 -07001091 final DiskInfo disk = mDisks.remove(cooked[1]);
1092 if (disk != null) {
1093 mCallbacks.notifyDiskDestroyed(disk);
1094 }
Jeff Sharkey48877892015-03-18 11:27:19 -07001095 break;
1096 }
San Mehat4270e1e2010-01-29 05:32:19 -08001097
Jeff Sharkey48877892015-03-18 11:27:19 -07001098 case VoldResponseCode.VOLUME_CREATED: {
Jeff Sharkey48877892015-03-18 11:27:19 -07001099 final String id = cooked[1];
1100 final int type = Integer.parseInt(cooked[2]);
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001101 final String diskId = TextUtils.nullIfEmpty(cooked[3]);
1102 final String partGuid = TextUtils.nullIfEmpty(cooked[4]);
1103
Jeff Sharkey27de30d2015-04-18 16:20:27 -07001104 final DiskInfo disk = mDisks.get(diskId);
Jeff Sharkey5af1835d2015-07-07 17:26:59 -07001105 final VolumeInfo vol = new VolumeInfo(id, type, disk, partGuid);
Jeff Sharkey48877892015-03-18 11:27:19 -07001106 mVolumes.put(id, vol);
1107 onVolumeCreatedLocked(vol);
1108 break;
1109 }
1110 case VoldResponseCode.VOLUME_STATE_CHANGED: {
1111 if (cooked.length != 3) break;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001112 final VolumeInfo vol = mVolumes.get(cooked[1]);
Jeff Sharkey48877892015-03-18 11:27:19 -07001113 if (vol != null) {
1114 final int oldState = vol.state;
1115 final int newState = Integer.parseInt(cooked[2]);
1116 vol.state = newState;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001117 onVolumeStateChangedLocked(vol, oldState, newState);
Jeff Sharkey48877892015-03-18 11:27:19 -07001118 }
1119 break;
1120 }
1121 case VoldResponseCode.VOLUME_FS_TYPE_CHANGED: {
1122 if (cooked.length != 3) break;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001123 final VolumeInfo vol = mVolumes.get(cooked[1]);
Jeff Sharkey48877892015-03-18 11:27:19 -07001124 if (vol != null) {
1125 vol.fsType = cooked[2];
1126 }
1127 break;
1128 }
1129 case VoldResponseCode.VOLUME_FS_UUID_CHANGED: {
1130 if (cooked.length != 3) break;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001131 final VolumeInfo vol = mVolumes.get(cooked[1]);
Jeff Sharkey48877892015-03-18 11:27:19 -07001132 if (vol != null) {
1133 vol.fsUuid = cooked[2];
1134 }
1135 break;
1136 }
1137 case VoldResponseCode.VOLUME_FS_LABEL_CHANGED: {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001138 final VolumeInfo vol = mVolumes.get(cooked[1]);
Jeff Sharkey48877892015-03-18 11:27:19 -07001139 if (vol != null) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -07001140 final StringBuilder builder = new StringBuilder();
1141 for (int i = 2; i < cooked.length; i++) {
1142 builder.append(cooked[i]).append(' ');
1143 }
1144 vol.fsLabel = builder.toString().trim();
Jeff Sharkey48877892015-03-18 11:27:19 -07001145 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001146 // TODO: notify listeners that label changed
Jeff Sharkey48877892015-03-18 11:27:19 -07001147 break;
1148 }
1149 case VoldResponseCode.VOLUME_PATH_CHANGED: {
1150 if (cooked.length != 3) break;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001151 final VolumeInfo vol = mVolumes.get(cooked[1]);
Jeff Sharkey48877892015-03-18 11:27:19 -07001152 if (vol != null) {
1153 vol.path = cooked[2];
1154 }
1155 break;
1156 }
Jeff Sharkey50a05452015-04-29 11:24:52 -07001157 case VoldResponseCode.VOLUME_INTERNAL_PATH_CHANGED: {
1158 if (cooked.length != 3) break;
1159 final VolumeInfo vol = mVolumes.get(cooked[1]);
1160 if (vol != null) {
1161 vol.internalPath = cooked[2];
1162 }
1163 break;
1164 }
Jeff Sharkey48877892015-03-18 11:27:19 -07001165 case VoldResponseCode.VOLUME_DESTROYED: {
1166 if (cooked.length != 2) break;
1167 mVolumes.remove(cooked[1]);
1168 break;
1169 }
San Mehat4270e1e2010-01-29 05:32:19 -08001170
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001171 case VoldResponseCode.MOVE_STATUS: {
1172 final int status = Integer.parseInt(cooked[1]);
1173 onMoveStatusLocked(status);
1174 break;
1175 }
Jeff Sharkey9756d752015-05-14 21:07:42 -07001176 case VoldResponseCode.BENCHMARK_RESULT: {
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07001177 if (cooked.length != 7) break;
1178 final String path = cooked[1];
1179 final String ident = cooked[2];
1180 final long create = Long.parseLong(cooked[3]);
1181 final long drop = Long.parseLong(cooked[4]);
1182 final long run = Long.parseLong(cooked[5]);
1183 final long destroy = Long.parseLong(cooked[6]);
1184
Jeff Sharkey9756d752015-05-14 21:07:42 -07001185 final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class);
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07001186 dropBox.addText(TAG_STORAGE_BENCHMARK, scrubPath(path)
1187 + " " + ident + " " + create + " " + run + " " + destroy);
1188
1189 final VolumeRecord rec = findRecordForPath(path);
1190 if (rec != null) {
1191 rec.lastBenchMillis = System.currentTimeMillis();
1192 writeSettingsLocked();
1193 }
1194
1195 break;
1196 }
1197 case VoldResponseCode.TRIM_RESULT: {
1198 if (cooked.length != 4) break;
1199 final String path = cooked[1];
1200 final long bytes = Long.parseLong(cooked[2]);
1201 final long time = Long.parseLong(cooked[3]);
1202
1203 final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class);
1204 dropBox.addText(TAG_STORAGE_TRIM, scrubPath(path)
1205 + " " + bytes + " " + time);
1206
1207 final VolumeRecord rec = findRecordForPath(path);
1208 if (rec != null) {
1209 rec.lastTrimMillis = System.currentTimeMillis();
1210 writeSettingsLocked();
1211 }
1212
Jeff Sharkey9756d752015-05-14 21:07:42 -07001213 break;
1214 }
1215
Jeff Sharkey48877892015-03-18 11:27:19 -07001216 default: {
1217 Slog.d(TAG, "Unhandled vold event " + code);
Mike Lockwooda5250c92011-05-23 13:44:04 -04001218 }
San Mehat4270e1e2010-01-29 05:32:19 -08001219 }
1220
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001221 return true;
San Mehat4270e1e2010-01-29 05:32:19 -08001222 }
1223
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001224 private void onDiskScannedLocked(DiskInfo disk) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001225 int volumeCount = 0;
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001226 for (int i = 0; i < mVolumes.size(); i++) {
1227 final VolumeInfo vol = mVolumes.valueAt(i);
1228 if (Objects.equals(disk.id, vol.getDiskId())) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001229 volumeCount++;
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001230 }
1231 }
1232
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001233 final Intent intent = new Intent(DiskInfo.ACTION_DISK_SCANNED);
Jeff Sharkey7732e1e2016-03-30 17:14:23 -06001234 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
1235 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001236 intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.id);
1237 intent.putExtra(DiskInfo.EXTRA_VOLUME_COUNT, volumeCount);
Jeff Sharkeyabc3e852015-08-03 14:41:13 -07001238 mHandler.obtainMessage(H_INTERNAL_BROADCAST, intent).sendToTarget();
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001239
1240 final CountDownLatch latch = mDiskScanLatches.remove(disk.id);
1241 if (latch != null) {
1242 latch.countDown();
1243 }
1244
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -07001245 disk.volumeCount = volumeCount;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001246 mCallbacks.notifyDiskScanned(disk, volumeCount);
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001247 }
1248
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001249 private void onVolumeCreatedLocked(VolumeInfo vol) {
Jeff Sharkey6855c482016-03-31 14:34:38 -06001250 if (mPms.isOnlyCoreApps()) {
1251 Slog.d(TAG, "System booted in core-only mode; ignoring volume " + vol.getId());
1252 return;
1253 }
1254
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001255 if (vol.type == VolumeInfo.TYPE_EMULATED) {
1256 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1257 final VolumeInfo privateVol = storage.findPrivateForEmulated(vol);
1258
1259 if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid)
1260 && VolumeInfo.ID_PRIVATE_INTERNAL.equals(privateVol.id)) {
1261 Slog.v(TAG, "Found primary storage at " + vol);
1262 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY;
1263 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
1264 mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
1265
1266 } else if (Objects.equals(privateVol.fsUuid, mPrimaryStorageUuid)) {
1267 Slog.v(TAG, "Found primary storage at " + vol);
1268 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY;
1269 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
1270 mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
1271 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001272
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001273 } else if (vol.type == VolumeInfo.TYPE_PUBLIC) {
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001274 // TODO: only look at first public partition
1275 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid)
1276 && vol.disk.isDefaultPrimary()) {
1277 Slog.v(TAG, "Found primary storage at " + vol);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07001278 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY;
1279 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
San Mehat4270e1e2010-01-29 05:32:19 -08001280 }
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07001281
1282 // Adoptable public disks are visible to apps, since they meet
1283 // public API requirement of being in a stable location.
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001284 if (vol.disk.isAdoptable()) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07001285 vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
1286 }
1287
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07001288 vol.mountUserId = ActivityManager.getCurrentUser();
Jeff Sharkey48877892015-03-18 11:27:19 -07001289 mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
San Mehat4270e1e2010-01-29 05:32:19 -08001290
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001291 } else if (vol.type == VolumeInfo.TYPE_PRIVATE) {
1292 mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
1293
San Mehat4270e1e2010-01-29 05:32:19 -08001294 } else {
Jeff Sharkey48877892015-03-18 11:27:19 -07001295 Slog.d(TAG, "Skipping automatic mounting of " + vol);
San Mehat4270e1e2010-01-29 05:32:19 -08001296 }
1297 }
1298
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001299 private boolean isBroadcastWorthy(VolumeInfo vol) {
1300 switch (vol.getType()) {
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001301 case VolumeInfo.TYPE_PRIVATE:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001302 case VolumeInfo.TYPE_PUBLIC:
1303 case VolumeInfo.TYPE_EMULATED:
1304 break;
1305 default:
1306 return false;
1307 }
1308
1309 switch (vol.getState()) {
1310 case VolumeInfo.STATE_MOUNTED:
1311 case VolumeInfo.STATE_MOUNTED_READ_ONLY:
1312 case VolumeInfo.STATE_EJECTING:
1313 case VolumeInfo.STATE_UNMOUNTED:
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001314 case VolumeInfo.STATE_UNMOUNTABLE:
Tony Mantlerf0d71052015-06-24 11:45:25 -07001315 case VolumeInfo.STATE_BAD_REMOVAL:
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001316 break;
1317 default:
1318 return false;
1319 }
1320
1321 return true;
1322 }
1323
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001324 private void onVolumeStateChangedLocked(VolumeInfo vol, int oldState, int newState) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001325 // Remember that we saw this volume so we're ready to accept user
1326 // metadata, or so we can annoy them when a private volume is ejected
1327 if (vol.isMountedReadable() && !TextUtils.isEmpty(vol.fsUuid)) {
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001328 VolumeRecord rec = mRecords.get(vol.fsUuid);
1329 if (rec == null) {
1330 rec = new VolumeRecord(vol.type, vol.fsUuid);
1331 rec.partGuid = vol.partGuid;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07001332 rec.createdMillis = System.currentTimeMillis();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001333 if (vol.type == VolumeInfo.TYPE_PRIVATE) {
1334 rec.nickname = vol.disk.getDescription();
1335 }
1336 mRecords.put(rec.fsUuid, rec);
1337 writeSettingsLocked();
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001338 } else {
1339 // Handle upgrade case where we didn't store partition GUID
1340 if (TextUtils.isEmpty(rec.partGuid)) {
1341 rec.partGuid = vol.partGuid;
1342 writeSettingsLocked();
1343 }
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001344 }
1345 }
1346
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001347 mCallbacks.notifyVolumeStateChanged(vol, oldState, newState);
1348
Yasuhiro Matsuda87a38b52015-07-24 22:10:16 +09001349 // Do not broadcast before boot has completed to avoid launching the
1350 // processes that receive the intent unnecessarily.
1351 if (mBootCompleted && isBroadcastWorthy(vol)) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001352 final Intent intent = new Intent(VolumeInfo.ACTION_VOLUME_STATE_CHANGED);
Jeff Sharkeyc7acac62015-06-12 16:16:56 -07001353 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.id);
1354 intent.putExtra(VolumeInfo.EXTRA_VOLUME_STATE, newState);
Tony Mantlerf0d71052015-06-24 11:45:25 -07001355 intent.putExtra(VolumeRecord.EXTRA_FS_UUID, vol.fsUuid);
Jeff Sharkey7732e1e2016-03-30 17:14:23 -06001356 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
1357 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Jeff Sharkeyabc3e852015-08-03 14:41:13 -07001358 mHandler.obtainMessage(H_INTERNAL_BROADCAST, intent).sendToTarget();
Jeff Sharkeye6c04f92015-04-18 21:38:05 -07001359 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001360
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001361 final String oldStateEnv = VolumeInfo.getEnvironmentForState(oldState);
1362 final String newStateEnv = VolumeInfo.getEnvironmentForState(newState);
Emily Bernier92aa5a22014-07-07 10:11:48 -04001363
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001364 if (!Objects.equals(oldStateEnv, newStateEnv)) {
1365 // Kick state changed event towards all started users. Any users
1366 // started after this point will trigger additional
1367 // user-specific broadcasts.
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001368 for (int userId : mSystemUnlockedUsers) {
Jeff Sharkey46349872015-07-28 10:49:47 -07001369 if (vol.isVisibleForRead(userId)) {
Svet Ganov6ee871e2015-07-10 14:29:33 -07001370 final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, false);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001371 mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget();
Jeff Sharkey48877892015-03-18 11:27:19 -07001372
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001373 mCallbacks.notifyStorageStateChanged(userVol.getPath(), oldStateEnv,
1374 newStateEnv);
San Mehat4270e1e2010-01-29 05:32:19 -08001375 }
1376 }
1377 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001378
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07001379 if (vol.type == VolumeInfo.TYPE_PUBLIC && vol.state == VolumeInfo.STATE_EJECTING) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001380 // TODO: this should eventually be handled by new ObbVolume state changes
1381 /*
1382 * Some OBBs might have been unmounted when this volume was
1383 * unmounted, so send a message to the handler to let it know to
1384 * remove those from the list of mounted OBBS.
1385 */
1386 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(
1387 OBB_FLUSH_MOUNT_STATE, vol.path));
1388 }
San Mehat4270e1e2010-01-29 05:32:19 -08001389 }
1390
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001391 private void onMoveStatusLocked(int status) {
1392 if (mMoveCallback == null) {
1393 Slog.w(TAG, "Odd, status but no move requested");
1394 return;
1395 }
1396
1397 // TODO: estimate remaining time
1398 try {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001399 mMoveCallback.onStatusChanged(-1, status, -1);
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001400 } catch (RemoteException ignored) {
1401 }
1402
1403 // We've finished copying and we're about to clean up old data, so
1404 // remember that move was successful if we get rebooted
1405 if (status == MOVE_STATUS_COPY_FINISHED) {
1406 Slog.d(TAG, "Move to " + mMoveTargetUuid + " copy phase finshed; persisting");
1407
1408 mPrimaryStorageUuid = mMoveTargetUuid;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001409 writeSettingsLocked();
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001410 }
1411
1412 if (PackageManager.isMoveStatusFinished(status)) {
1413 Slog.d(TAG, "Move to " + mMoveTargetUuid + " finished with status " + status);
1414
1415 mMoveCallback = null;
1416 mMoveTargetUuid = null;
1417 }
1418 }
1419
Jeff Sharkey48877892015-03-18 11:27:19 -07001420 private void enforcePermission(String perm) {
1421 mContext.enforceCallingOrSelfPermission(perm, perm);
Mike Lockwooda5250c92011-05-23 13:44:04 -04001422 }
1423
Jeff Sharkey2e606d72015-07-27 14:19:54 -07001424 /**
1425 * Decide if volume is mountable per device policies.
1426 */
1427 private boolean isMountDisallowed(VolumeInfo vol) {
1428 if (vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_PRIVATE) {
1429 final UserManager userManager = mContext.getSystemService(UserManager.class);
1430 return userManager.hasUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
1431 Binder.getCallingUserHandle());
1432 } else {
1433 return false;
Emily Bernier92aa5a22014-07-07 10:11:48 -04001434 }
1435 }
1436
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001437 private void enforceAdminUser() {
1438 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1439 final int callingUserId = UserHandle.getCallingUserId();
1440 boolean isAdmin;
1441 long token = Binder.clearCallingIdentity();
1442 try {
1443 isAdmin = um.getUserInfo(callingUserId).isAdmin();
1444 } finally {
1445 Binder.restoreCallingIdentity(token);
1446 }
1447 if (!isAdmin) {
1448 throw new SecurityException("Only admin users can adopt sd cards");
1449 }
1450 }
1451
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001452 /**
San Mehat207e5382010-02-04 20:46:54 -08001453 * Constructs a new MountService instance
1454 *
1455 * @param context Binder context for this service
1456 */
1457 public MountService(Context context) {
Christopher Tated417d622013-08-19 16:14:25 -07001458 sSelf = this;
1459
San Mehat207e5382010-02-04 20:46:54 -08001460 mContext = context;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001461 mCallbacks = new Callbacks(FgThread.get().getLooper());
Jeff Sharkeyb9fe5372015-12-03 15:23:08 -07001462 mLockPatternUtils = new LockPatternUtils(mContext);
San Mehat207e5382010-02-04 20:46:54 -08001463
San Mehat207e5382010-02-04 20:46:54 -08001464 // XXX: This will go away soon in favor of IMountServiceObserver
1465 mPms = (PackageManagerService) ServiceManager.getService("package");
1466
Dianne Hackbornefa92b22013-05-03 14:11:43 -07001467 HandlerThread hthread = new HandlerThread(TAG);
1468 hthread.start();
1469 mHandler = new MountServiceHandler(hthread.getLooper());
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001470
Kenny Roota02b8b02010-08-05 16:14:17 -07001471 // Add OBB Action Handler to MountService thread.
Dianne Hackborn8d044e82013-04-30 17:24:15 -07001472 mObbActionHandler = new ObbActionHandler(IoThread.get().getLooper());
Kenny Roota02b8b02010-08-05 16:14:17 -07001473
Christopher Tate7265abe2014-11-21 13:54:45 -08001474 // Initialize the last-fstrim tracking if necessary
1475 File dataDir = Environment.getDataDirectory();
1476 File systemDir = new File(dataDir, "system");
1477 mLastMaintenanceFile = new File(systemDir, LAST_FSTRIM_FILE);
1478 if (!mLastMaintenanceFile.exists()) {
1479 // Not setting mLastMaintenance here means that we will force an
1480 // fstrim during reboot following the OTA that installs this code.
1481 try {
1482 (new FileOutputStream(mLastMaintenanceFile)).close();
1483 } catch (IOException e) {
1484 Slog.e(TAG, "Unable to create fstrim record " + mLastMaintenanceFile.getPath());
1485 }
1486 } else {
1487 mLastMaintenance = mLastMaintenanceFile.lastModified();
1488 }
1489
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001490 mSettingsFile = new AtomicFile(
Jeff Sharkey8212ae02016-02-10 14:46:43 -07001491 new File(Environment.getDataSystemDirectory(), "storage.xml"));
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001492
1493 synchronized (mLock) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001494 readSettingsLocked();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001495 }
1496
Svet Ganov6ee871e2015-07-10 14:29:33 -07001497 LocalServices.addService(MountServiceInternal.class, mMountServiceInternal);
1498
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001499 /*
Kenny Root305bcbf2010-09-03 07:56:38 -07001500 * Create the connection to vold with a maximum queue of twice the
1501 * amount of containers we'd ever expect to have. This keeps an
1502 * "asec list" from blocking a thread repeatedly.
1503 */
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07001504
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001505 mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG, 25,
1506 null);
Jeff Sharkey48877892015-03-18 11:27:19 -07001507 mConnector.setDebug(true);
Jeff Sharkey8948c012015-11-03 12:33:54 -08001508 mConnector.setWarnIfHeld(mLock);
Jeff Sharkeycd575992016-03-29 14:12:49 -06001509 mConnectorThread = new Thread(mConnector, VOLD_TAG);
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07001510
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07001511 // Reuse parameters from first connector since they are tested and safe
1512 mCryptConnector = new NativeDaemonConnector(this, "cryptd",
1513 MAX_CONTAINERS * 2, CRYPTD_TAG, 25, null);
1514 mCryptConnector.setDebug(true);
Jeff Sharkeycd575992016-03-29 14:12:49 -06001515 mCryptConnectorThread = new Thread(mCryptConnector, CRYPTD_TAG);
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07001516
Jeff Sharkeybcd262d2015-06-10 09:41:17 -07001517 final IntentFilter userFilter = new IntentFilter();
1518 userFilter.addAction(Intent.ACTION_USER_ADDED);
1519 userFilter.addAction(Intent.ACTION_USER_REMOVED);
1520 mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler);
1521
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001522 synchronized (mLock) {
1523 addInternalVolumeLocked();
1524 }
Amith Yamasania7892482015-08-07 11:09:05 -07001525
Kenny Root07714d42011-08-17 17:49:28 -07001526 // Add ourself to the Watchdog monitors if enabled.
1527 if (WATCHDOG_ENABLE) {
1528 Watchdog.getInstance().addMonitor(this);
1529 }
San Mehat207e5382010-02-04 20:46:54 -08001530 }
1531
Jeff Sharkeycd575992016-03-29 14:12:49 -06001532 private void start() {
1533 mConnectorThread.start();
1534 mCryptConnectorThread.start();
1535 }
1536
Jeff Sharkey56e62932015-03-21 20:41:00 -07001537 private void systemReady() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001538 mSystemReady = true;
1539 mHandler.obtainMessage(H_SYSTEM_READY).sendToTarget();
1540 }
1541
Yasuhiro Matsuda87a38b52015-07-24 22:10:16 +09001542 private void bootCompleted() {
1543 mBootCompleted = true;
1544 }
1545
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001546 private String getDefaultPrimaryStorageUuid() {
1547 if (SystemProperties.getBoolean(StorageManager.PROP_PRIMARY_PHYSICAL, false)) {
1548 return StorageManager.UUID_PRIMARY_PHYSICAL;
1549 } else {
1550 return StorageManager.UUID_PRIVATE_INTERNAL;
1551 }
1552 }
1553
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001554 private void readSettingsLocked() {
1555 mRecords.clear();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001556 mPrimaryStorageUuid = getDefaultPrimaryStorageUuid();
Jeff Sharkey4c099d02015-05-15 13:45:00 -07001557 mForceAdoptable = false;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001558
1559 FileInputStream fis = null;
1560 try {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001561 fis = mSettingsFile.openRead();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001562 final XmlPullParser in = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001563 in.setInput(fis, StandardCharsets.UTF_8.name());
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001564
1565 int type;
1566 while ((type = in.next()) != END_DOCUMENT) {
1567 if (type == START_TAG) {
1568 final String tag = in.getName();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001569 if (TAG_VOLUMES.equals(tag)) {
1570 final int version = readIntAttribute(in, ATTR_VERSION, VERSION_INIT);
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001571 final boolean primaryPhysical = SystemProperties.getBoolean(
1572 StorageManager.PROP_PRIMARY_PHYSICAL, false);
1573 final boolean validAttr = (version >= VERSION_FIX_PRIMARY)
1574 || (version >= VERSION_ADD_PRIMARY && !primaryPhysical);
1575 if (validAttr) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001576 mPrimaryStorageUuid = readStringAttribute(in,
1577 ATTR_PRIMARY_STORAGE_UUID);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001578 }
Jeff Sharkey4c099d02015-05-15 13:45:00 -07001579 mForceAdoptable = readBooleanAttribute(in, ATTR_FORCE_ADOPTABLE, false);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001580
1581 } else if (TAG_VOLUME.equals(tag)) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001582 final VolumeRecord rec = readVolumeRecord(in);
1583 mRecords.put(rec.fsUuid, rec);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001584 }
1585 }
1586 }
1587 } catch (FileNotFoundException e) {
1588 // Missing metadata is okay, probably first boot
1589 } catch (IOException e) {
1590 Slog.wtf(TAG, "Failed reading metadata", e);
1591 } catch (XmlPullParserException e) {
1592 Slog.wtf(TAG, "Failed reading metadata", e);
1593 } finally {
1594 IoUtils.closeQuietly(fis);
1595 }
1596 }
1597
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001598 private void writeSettingsLocked() {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001599 FileOutputStream fos = null;
1600 try {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001601 fos = mSettingsFile.startWrite();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001602
1603 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001604 out.setOutput(fos, StandardCharsets.UTF_8.name());
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001605 out.startDocument(null, true);
1606 out.startTag(null, TAG_VOLUMES);
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001607 writeIntAttribute(out, ATTR_VERSION, VERSION_FIX_PRIMARY);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001608 writeStringAttribute(out, ATTR_PRIMARY_STORAGE_UUID, mPrimaryStorageUuid);
Jeff Sharkey4c099d02015-05-15 13:45:00 -07001609 writeBooleanAttribute(out, ATTR_FORCE_ADOPTABLE, mForceAdoptable);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001610 final int size = mRecords.size();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001611 for (int i = 0; i < size; i++) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001612 final VolumeRecord rec = mRecords.valueAt(i);
1613 writeVolumeRecord(out, rec);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001614 }
1615 out.endTag(null, TAG_VOLUMES);
1616 out.endDocument();
1617
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001618 mSettingsFile.finishWrite(fos);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001619 } catch (IOException e) {
1620 if (fos != null) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001621 mSettingsFile.failWrite(fos);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001622 }
1623 }
1624 }
1625
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001626 public static VolumeRecord readVolumeRecord(XmlPullParser in) throws IOException {
1627 final int type = readIntAttribute(in, ATTR_TYPE);
1628 final String fsUuid = readStringAttribute(in, ATTR_FS_UUID);
1629 final VolumeRecord meta = new VolumeRecord(type, fsUuid);
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001630 meta.partGuid = readStringAttribute(in, ATTR_PART_GUID);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001631 meta.nickname = readStringAttribute(in, ATTR_NICKNAME);
1632 meta.userFlags = readIntAttribute(in, ATTR_USER_FLAGS);
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07001633 meta.createdMillis = readLongAttribute(in, ATTR_CREATED_MILLIS);
1634 meta.lastTrimMillis = readLongAttribute(in, ATTR_LAST_TRIM_MILLIS);
1635 meta.lastBenchMillis = readLongAttribute(in, ATTR_LAST_BENCH_MILLIS);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001636 return meta;
1637 }
1638
1639 public static void writeVolumeRecord(XmlSerializer out, VolumeRecord rec) throws IOException {
1640 out.startTag(null, TAG_VOLUME);
1641 writeIntAttribute(out, ATTR_TYPE, rec.type);
1642 writeStringAttribute(out, ATTR_FS_UUID, rec.fsUuid);
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001643 writeStringAttribute(out, ATTR_PART_GUID, rec.partGuid);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001644 writeStringAttribute(out, ATTR_NICKNAME, rec.nickname);
1645 writeIntAttribute(out, ATTR_USER_FLAGS, rec.userFlags);
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07001646 writeLongAttribute(out, ATTR_CREATED_MILLIS, rec.createdMillis);
1647 writeLongAttribute(out, ATTR_LAST_TRIM_MILLIS, rec.lastTrimMillis);
1648 writeLongAttribute(out, ATTR_LAST_BENCH_MILLIS, rec.lastBenchMillis);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001649 out.endTag(null, TAG_VOLUME);
1650 }
1651
San Mehat207e5382010-02-04 20:46:54 -08001652 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001653 * Exposed API calls below here
1654 */
1655
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001656 @Override
San Mehat4270e1e2010-01-29 05:32:19 -08001657 public void registerListener(IMountServiceListener listener) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001658 mCallbacks.register(listener);
San Mehat4270e1e2010-01-29 05:32:19 -08001659 }
1660
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001661 @Override
San Mehat4270e1e2010-01-29 05:32:19 -08001662 public void unregisterListener(IMountServiceListener listener) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001663 mCallbacks.unregister(listener);
San Mehat4270e1e2010-01-29 05:32:19 -08001664 }
1665
Jeff Sharkey48877892015-03-18 11:27:19 -07001666 @Override
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001667 public void shutdown(final IMountShutdownObserver observer) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001668 enforcePermission(android.Manifest.permission.SHUTDOWN);
San Mehat4270e1e2010-01-29 05:32:19 -08001669
San Mehata5078592010-03-25 09:36:54 -07001670 Slog.i(TAG, "Shutting down");
Jeff Sharkey48877892015-03-18 11:27:19 -07001671 mHandler.obtainMessage(H_SHUTDOWN, observer).sendToTarget();
San Mehat4270e1e2010-01-29 05:32:19 -08001672 }
1673
Jeff Sharkey48877892015-03-18 11:27:19 -07001674 @Override
San Mehatb1043402010-02-05 08:26:50 -08001675 public boolean isUsbMassStorageConnected() {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001676 throw new UnsupportedOperationException();
San Mehatb1043402010-02-05 08:26:50 -08001677 }
1678
Jeff Sharkey48877892015-03-18 11:27:19 -07001679 @Override
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001680 public void setUsbMassStorageEnabled(boolean enable) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001681 throw new UnsupportedOperationException();
San Mehatb1043402010-02-05 08:26:50 -08001682 }
1683
Jeff Sharkey48877892015-03-18 11:27:19 -07001684 @Override
San Mehatb1043402010-02-05 08:26:50 -08001685 public boolean isUsbMassStorageEnabled() {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001686 throw new UnsupportedOperationException();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001688
Jeff Sharkey48877892015-03-18 11:27:19 -07001689 @Override
San Mehat4270e1e2010-01-29 05:32:19 -08001690 public String getVolumeState(String mountPoint) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001691 throw new UnsupportedOperationException();
San Mehat7fd0fee2009-12-17 07:12:23 -08001692 }
1693
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001694 @Override
Kenny Roote1ff2142010-10-12 11:20:01 -07001695 public boolean isExternalStorageEmulated() {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001696 throw new UnsupportedOperationException();
Kenny Roote1ff2142010-10-12 11:20:01 -07001697 }
1698
Jeff Sharkey48877892015-03-18 11:27:19 -07001699 @Override
San Mehat4270e1e2010-01-29 05:32:19 -08001700 public int mountVolume(String path) {
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07001701 mount(findVolumeIdForPathOrThrow(path));
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001702 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 }
1704
Jeff Sharkey48877892015-03-18 11:27:19 -07001705 @Override
Ben Komalo13c71972011-09-07 16:35:56 -07001706 public void unmountVolume(String path, boolean force, boolean removeEncryption) {
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07001707 unmount(findVolumeIdForPathOrThrow(path));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
1709
Jeff Sharkey48877892015-03-18 11:27:19 -07001710 @Override
San Mehat4270e1e2010-01-29 05:32:19 -08001711 public int formatVolume(String path) {
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07001712 format(findVolumeIdForPathOrThrow(path));
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001713 return 0;
1714 }
1715
1716 @Override
1717 public void mount(String volId) {
1718 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1719 waitForReady();
1720
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07001721 final VolumeInfo vol = findVolumeByIdOrThrow(volId);
Jeff Sharkey2e606d72015-07-27 14:19:54 -07001722 if (isMountDisallowed(vol)) {
1723 throw new SecurityException("Mounting " + volId + " restricted by policy");
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001724 }
1725 try {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07001726 mConnector.execute("volume", "mount", vol.id, vol.mountFlags, vol.mountUserId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001727 } catch (NativeDaemonConnectorException e) {
1728 throw e.rethrowAsParcelableException();
1729 }
1730 }
1731
1732 @Override
1733 public void unmount(String volId) {
1734 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1735 waitForReady();
1736
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07001737 final VolumeInfo vol = findVolumeByIdOrThrow(volId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001738
1739 // TODO: expand PMS to know about multiple volumes
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001740 if (vol.isPrimaryPhysical()) {
1741 final long ident = Binder.clearCallingIdentity();
1742 try {
1743 synchronized (mUnmountLock) {
1744 mUnmountSignal = new CountDownLatch(1);
1745 mPms.updateExternalMediaStatus(false, true);
1746 waitForLatch(mUnmountSignal, "mUnmountSignal");
1747 mUnmountSignal = null;
1748 }
1749 } finally {
1750 Binder.restoreCallingIdentity(ident);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001751 }
1752 }
1753
1754 try {
1755 mConnector.execute("volume", "unmount", vol.id);
1756 } catch (NativeDaemonConnectorException e) {
1757 throw e.rethrowAsParcelableException();
1758 }
1759 }
1760
1761 @Override
1762 public void format(String volId) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001763 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001764 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001765
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07001766 final VolumeInfo vol = findVolumeByIdOrThrow(volId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001767 try {
Jeff Sharkey4e83cc92015-05-27 14:38:39 -07001768 mConnector.execute("volume", "format", vol.id, "auto");
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001769 } catch (NativeDaemonConnectorException e) {
1770 throw e.rethrowAsParcelableException();
Jeff Sharkey48877892015-03-18 11:27:19 -07001771 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001772 }
1773
1774 @Override
Jeff Sharkey9756d752015-05-14 21:07:42 -07001775 public long benchmark(String volId) {
1776 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
1777 waitForReady();
1778
1779 try {
Jeff Sharkey14cbe522015-07-08 14:06:37 -07001780 // TODO: make benchmark async so we don't block other commands
1781 final NativeDaemonEvent res = mConnector.execute(3 * DateUtils.MINUTE_IN_MILLIS,
1782 "volume", "benchmark", volId);
Jeff Sharkey9756d752015-05-14 21:07:42 -07001783 return Long.parseLong(res.getMessage());
Todd Kennedy8101ee62015-06-23 13:35:28 -07001784 } catch (NativeDaemonTimeoutException e) {
1785 return Long.MAX_VALUE;
Jeff Sharkey9756d752015-05-14 21:07:42 -07001786 } catch (NativeDaemonConnectorException e) {
1787 throw e.rethrowAsParcelableException();
1788 }
1789 }
1790
1791 @Override
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001792 public void partitionPublic(String diskId) {
1793 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
1794 waitForReady();
1795
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001796 final CountDownLatch latch = findOrCreateDiskScanLatch(diskId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001797 try {
1798 mConnector.execute("volume", "partition", diskId, "public");
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -07001799 waitForLatch(latch, "partitionPublic", 3 * DateUtils.MINUTE_IN_MILLIS);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001800 } catch (NativeDaemonConnectorException e) {
1801 throw e.rethrowAsParcelableException();
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -07001802 } catch (TimeoutException e) {
1803 throw new IllegalStateException(e);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001804 }
1805 }
1806
1807 @Override
1808 public void partitionPrivate(String diskId) {
1809 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001810 enforceAdminUser();
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001811 waitForReady();
1812
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001813 final CountDownLatch latch = findOrCreateDiskScanLatch(diskId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001814 try {
1815 mConnector.execute("volume", "partition", diskId, "private");
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -07001816 waitForLatch(latch, "partitionPrivate", 3 * DateUtils.MINUTE_IN_MILLIS);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001817 } catch (NativeDaemonConnectorException e) {
1818 throw e.rethrowAsParcelableException();
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -07001819 } catch (TimeoutException e) {
1820 throw new IllegalStateException(e);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001821 }
1822 }
1823
1824 @Override
1825 public void partitionMixed(String diskId, int ratio) {
1826 enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
Amith Yamasani462ac3a2015-06-30 14:21:01 -07001827 enforceAdminUser();
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001828 waitForReady();
1829
Jeff Sharkeyeba260d2015-04-19 14:35:16 -07001830 final CountDownLatch latch = findOrCreateDiskScanLatch(diskId);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001831 try {
1832 mConnector.execute("volume", "partition", diskId, "mixed", ratio);
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -07001833 waitForLatch(latch, "partitionMixed", 3 * DateUtils.MINUTE_IN_MILLIS);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001834 } catch (NativeDaemonConnectorException e) {
1835 throw e.rethrowAsParcelableException();
Jeff Sharkeyedcdaf62015-07-09 09:45:36 -07001836 } catch (TimeoutException e) {
1837 throw new IllegalStateException(e);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07001838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
1840
Jeff Sharkey48877892015-03-18 11:27:19 -07001841 @Override
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001842 public void setVolumeNickname(String fsUuid, String nickname) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001843 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1844 waitForReady();
1845
Jeff Sharkey50a05452015-04-29 11:24:52 -07001846 Preconditions.checkNotNull(fsUuid);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001847 synchronized (mLock) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001848 final VolumeRecord rec = mRecords.get(fsUuid);
1849 rec.nickname = nickname;
Jeff Sharkey50a05452015-04-29 11:24:52 -07001850 mCallbacks.notifyVolumeRecordChanged(rec);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001851 writeSettingsLocked();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001852 }
1853 }
1854
1855 @Override
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001856 public void setVolumeUserFlags(String fsUuid, int flags, int mask) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001857 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1858 waitForReady();
1859
Jeff Sharkey50a05452015-04-29 11:24:52 -07001860 Preconditions.checkNotNull(fsUuid);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001861 synchronized (mLock) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001862 final VolumeRecord rec = mRecords.get(fsUuid);
1863 rec.userFlags = (rec.userFlags & ~mask) | (flags & mask);
Jeff Sharkey50a05452015-04-29 11:24:52 -07001864 mCallbacks.notifyVolumeRecordChanged(rec);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001865 writeSettingsLocked();
1866 }
1867 }
1868
1869 @Override
1870 public void forgetVolume(String fsUuid) {
1871 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1872 waitForReady();
1873
Jeff Sharkey50a05452015-04-29 11:24:52 -07001874 Preconditions.checkNotNull(fsUuid);
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001875
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001876 synchronized (mLock) {
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001877 final VolumeRecord rec = mRecords.remove(fsUuid);
1878 if (rec != null && !TextUtils.isEmpty(rec.partGuid)) {
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001879 mHandler.obtainMessage(H_PARTITION_FORGET, rec.partGuid).sendToTarget();
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001880 }
1881 mCallbacks.notifyVolumeForgotten(fsUuid);
Jeff Sharkey50a05452015-04-29 11:24:52 -07001882
1883 // If this had been primary storage, revert back to internal and
1884 // reset vold so we bind into new volume into place.
1885 if (Objects.equals(mPrimaryStorageUuid, fsUuid)) {
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001886 mPrimaryStorageUuid = getDefaultPrimaryStorageUuid();
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001887 mHandler.obtainMessage(H_RESET).sendToTarget();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001888 }
1889
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07001890 writeSettingsLocked();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07001891 }
1892 }
1893
Jeff Sharkey7d2af542015-05-12 15:27:15 -07001894 @Override
1895 public void forgetAllVolumes() {
1896 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1897 waitForReady();
1898
Jeff Sharkey50a05452015-04-29 11:24:52 -07001899 synchronized (mLock) {
1900 for (int i = 0; i < mRecords.size(); i++) {
1901 final String fsUuid = mRecords.keyAt(i);
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001902 final VolumeRecord rec = mRecords.valueAt(i);
1903 if (!TextUtils.isEmpty(rec.partGuid)) {
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001904 mHandler.obtainMessage(H_PARTITION_FORGET, rec.partGuid).sendToTarget();
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001905 }
Jeff Sharkey50a05452015-04-29 11:24:52 -07001906 mCallbacks.notifyVolumeForgotten(fsUuid);
1907 }
Jeff Sharkey50a05452015-04-29 11:24:52 -07001908 mRecords.clear();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001909
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001910 if (!Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid)) {
1911 mPrimaryStorageUuid = getDefaultPrimaryStorageUuid();
1912 }
1913
1914 writeSettingsLocked();
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08001915 mHandler.obtainMessage(H_RESET).sendToTarget();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001916 }
1917 }
1918
Jeff Sharkey5cc0df22015-06-17 19:44:05 -07001919 private void forgetPartition(String partGuid) {
1920 try {
1921 mConnector.execute("volume", "forget_partition", partGuid);
1922 } catch (NativeDaemonConnectorException e) {
1923 Slog.w(TAG, "Failed to forget key for " + partGuid + ": " + e);
1924 }
1925 }
1926
Svet Ganov6ee871e2015-07-10 14:29:33 -07001927 private void remountUidExternalStorage(int uid, int mode) {
Jeff Sharkey9527b222015-06-24 15:24:48 -07001928 waitForReady();
1929
Svet Ganov6ee871e2015-07-10 14:29:33 -07001930 String modeName = "none";
1931 switch (mode) {
1932 case Zygote.MOUNT_EXTERNAL_DEFAULT: {
1933 modeName = "default";
1934 } break;
1935
1936 case Zygote.MOUNT_EXTERNAL_READ: {
1937 modeName = "read";
1938 } break;
1939
1940 case Zygote.MOUNT_EXTERNAL_WRITE: {
1941 modeName = "write";
1942 } break;
Jeff Sharkey9527b222015-06-24 15:24:48 -07001943 }
1944
1945 try {
Svet Ganov6ee871e2015-07-10 14:29:33 -07001946 mConnector.execute("volume", "remount_uid", uid, modeName);
Jeff Sharkey9527b222015-06-24 15:24:48 -07001947 } catch (NativeDaemonConnectorException e) {
Svet Ganov6ee871e2015-07-10 14:29:33 -07001948 Slog.w(TAG, "Failed to remount UID " + uid + " as " + modeName + ": " + e);
Jeff Sharkey9527b222015-06-24 15:24:48 -07001949 }
1950 }
1951
1952 @Override
Jeff Sharkey4c099d02015-05-15 13:45:00 -07001953 public void setDebugFlags(int flags, int mask) {
1954 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1955 waitForReady();
1956
Jeff Sharkeyba512352015-11-12 20:17:45 -08001957 if ((mask & StorageManager.DEBUG_EMULATE_FBE) != 0) {
Paul Lawrence20be5d62016-02-26 13:51:17 -08001958 if (StorageManager.isFileEncryptedNativeOnly()) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001959 throw new IllegalStateException(
1960 "Emulation not available on device with native FBE");
1961 }
Jeff Sharkey5a785162016-03-21 13:02:06 -06001962 if (mLockPatternUtils.isCredentialRequiredToDecrypt(false)) {
1963 throw new IllegalStateException(
1964 "Emulation requires disabling 'Secure start-up' in Settings > Security");
1965 }
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001966
Jeff Sharkey1176e512016-02-29 17:01:26 -07001967 final long token = Binder.clearCallingIdentity();
1968 try {
1969 final boolean emulateFbe = (flags & StorageManager.DEBUG_EMULATE_FBE) != 0;
1970 SystemProperties.set(StorageManager.PROP_EMULATE_FBE, Boolean.toString(emulateFbe));
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07001971
Jeff Sharkey1176e512016-02-29 17:01:26 -07001972 // Perform hard reboot to kick policy into place
1973 mContext.getSystemService(PowerManager.class).reboot(null);
1974 } finally {
1975 Binder.restoreCallingIdentity(token);
1976 }
Jeff Sharkeyba512352015-11-12 20:17:45 -08001977 }
Jeff Sharkey4c099d02015-05-15 13:45:00 -07001978
Jeff Sharkeyba512352015-11-12 20:17:45 -08001979 if ((mask & StorageManager.DEBUG_FORCE_ADOPTABLE) != 0) {
1980 synchronized (mLock) {
1981 mForceAdoptable = (flags & StorageManager.DEBUG_FORCE_ADOPTABLE) != 0;
1982
1983 writeSettingsLocked();
1984 mHandler.obtainMessage(H_RESET).sendToTarget();
1985 }
Jeff Sharkey4c099d02015-05-15 13:45:00 -07001986 }
1987 }
1988
1989 @Override
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001990 public String getPrimaryStorageUuid() {
1991 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1992 waitForReady();
1993
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001994 synchronized (mLock) {
1995 return mPrimaryStorageUuid;
1996 }
1997 }
1998
1999 @Override
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002000 public void setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback) {
2001 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
2002 waitForReady();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002003
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002004 synchronized (mLock) {
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002005 if (Objects.equals(mPrimaryStorageUuid, volumeUuid)) {
2006 throw new IllegalArgumentException("Primary storage already at " + volumeUuid);
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002007 }
2008
2009 if (mMoveCallback != null) {
2010 throw new IllegalStateException("Move already in progress");
2011 }
2012 mMoveCallback = callback;
2013 mMoveTargetUuid = volumeUuid;
2014
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002015 // When moving to/from primary physical volume, we probably just nuked
2016 // the current storage location, so we have nothing to move.
2017 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid)
2018 || Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) {
2019 Slog.d(TAG, "Skipping move to/from primary physical");
2020 onMoveStatusLocked(MOVE_STATUS_COPY_FINISHED);
2021 onMoveStatusLocked(PackageManager.MOVE_SUCCEEDED);
Jeff Sharkey5a9bb742015-11-03 10:15:57 -08002022 mHandler.obtainMessage(H_RESET).sendToTarget();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002023
2024 } else {
Jeff Sharkeyef10ee02015-07-05 14:17:27 -07002025 final VolumeInfo from = findStorageForUuid(mPrimaryStorageUuid);
2026 final VolumeInfo to = findStorageForUuid(volumeUuid);
2027
2028 if (from == null) {
2029 Slog.w(TAG, "Failing move due to missing from volume " + mPrimaryStorageUuid);
2030 onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR);
2031 return;
2032 } else if (to == null) {
2033 Slog.w(TAG, "Failing move due to missing to volume " + volumeUuid);
2034 onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR);
2035 return;
2036 }
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002037
2038 try {
2039 mConnector.execute("volume", "move_storage", from.id, to.id);
2040 } catch (NativeDaemonConnectorException e) {
2041 throw e.rethrowAsParcelableException();
2042 }
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002043 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002044 }
2045 }
2046
2047 @Override
Mike Lockwoodecedfdc2011-06-08 15:11:59 -07002048 public int[] getStorageUsers(String path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002049 enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatc1b4ce92010-02-16 17:13:03 -08002050 waitForReady();
2051 try {
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002052 final String[] r = NativeDaemonEvent.filterMessageList(
2053 mConnector.executeForList("storage", "users", path),
2054 VoldResponseCode.StorageUsersListResult);
2055
San Mehatc1b4ce92010-02-16 17:13:03 -08002056 // FMT: <pid> <process name>
2057 int[] data = new int[r.length];
2058 for (int i = 0; i < r.length; i++) {
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002059 String[] tok = r[i].split(" ");
San Mehatc1b4ce92010-02-16 17:13:03 -08002060 try {
2061 data[i] = Integer.parseInt(tok[0]);
2062 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07002063 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08002064 return new int[0];
2065 }
2066 }
2067 return data;
2068 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07002069 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08002070 return new int[0];
2071 }
2072 }
2073
San Mehatb1043402010-02-05 08:26:50 -08002074 private void warnOnNotMounted() {
Jeff Sharkey48877892015-03-18 11:27:19 -07002075 synchronized (mLock) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07002076 for (int i = 0; i < mVolumes.size(); i++) {
2077 final VolumeInfo vol = mVolumes.valueAt(i);
Jeff Sharkey27de30d2015-04-18 16:20:27 -07002078 if (vol.isPrimary() && vol.isMountedWritable()) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002079 // Cool beans, we have a mounted primary volume
2080 return;
2081 }
Jeff Sharkey32ee8312012-09-30 13:21:31 -07002082 }
San Mehatb1043402010-02-05 08:26:50 -08002083 }
Jeff Sharkey48877892015-03-18 11:27:19 -07002084
2085 Slog.w(TAG, "No primary storage mounted!");
San Mehatb1043402010-02-05 08:26:50 -08002086 }
2087
San Mehat4270e1e2010-01-29 05:32:19 -08002088 public String[] getSecureContainerList() {
Jeff Sharkey48877892015-03-18 11:27:19 -07002089 enforcePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08002090 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08002091 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08002092
San Mehat4270e1e2010-01-29 05:32:19 -08002093 try {
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002094 return NativeDaemonEvent.filterMessageList(
2095 mConnector.executeForList("asec", "list"), VoldResponseCode.AsecListResult);
San Mehat4270e1e2010-01-29 05:32:19 -08002096 } catch (NativeDaemonConnectorException e) {
2097 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 }
2099 }
San Mehat36972292010-01-06 11:06:32 -08002100
Kenny Root6dceb882012-04-12 14:23:49 -07002101 public int createSecureContainer(String id, int sizeMb, String fstype, String key,
2102 int ownerUid, boolean external) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002103 enforcePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08002104 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08002105 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08002106
San Mehatb1043402010-02-05 08:26:50 -08002107 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08002108 try {
Jeff Sharkey56cd6462013-06-07 15:09:15 -07002109 mConnector.execute("asec", "create", id, sizeMb, fstype, new SensitiveArg(key),
2110 ownerUid, external ? "1" : "0");
San Mehat4270e1e2010-01-29 05:32:19 -08002111 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08002112 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08002113 }
San Mehata181b212010-02-11 06:50:20 -08002114
2115 if (rc == StorageResultCode.OperationSucceeded) {
2116 synchronized (mAsecMountSet) {
2117 mAsecMountSet.add(id);
2118 }
2119 }
San Mehat4270e1e2010-01-29 05:32:19 -08002120 return rc;
San Mehat36972292010-01-06 11:06:32 -08002121 }
2122
Jeff Sharkey941a8ba2014-08-20 16:26:32 -07002123 @Override
2124 public int resizeSecureContainer(String id, int sizeMb, String key) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002125 enforcePermission(android.Manifest.permission.ASEC_CREATE);
Jeff Sharkey941a8ba2014-08-20 16:26:32 -07002126 waitForReady();
2127 warnOnNotMounted();
2128
2129 int rc = StorageResultCode.OperationSucceeded;
2130 try {
2131 mConnector.execute("asec", "resize", id, sizeMb, new SensitiveArg(key));
2132 } catch (NativeDaemonConnectorException e) {
2133 rc = StorageResultCode.OperationFailedInternalError;
2134 }
2135 return rc;
2136 }
2137
San Mehat4270e1e2010-01-29 05:32:19 -08002138 public int finalizeSecureContainer(String id) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002139 enforcePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08002140 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08002141
San Mehatb1043402010-02-05 08:26:50 -08002142 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08002143 try {
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002144 mConnector.execute("asec", "finalize", id);
San Mehata181b212010-02-11 06:50:20 -08002145 /*
2146 * Finalization does a remount, so no need
2147 * to update mAsecMountSet
2148 */
San Mehat4270e1e2010-01-29 05:32:19 -08002149 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08002150 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08002151 }
San Mehat4270e1e2010-01-29 05:32:19 -08002152 return rc;
San Mehat36972292010-01-06 11:06:32 -08002153 }
2154
Kenny Root6dceb882012-04-12 14:23:49 -07002155 public int fixPermissionsSecureContainer(String id, int gid, String filename) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002156 enforcePermission(android.Manifest.permission.ASEC_CREATE);
Kenny Root6dceb882012-04-12 14:23:49 -07002157 warnOnNotMounted();
2158
2159 int rc = StorageResultCode.OperationSucceeded;
2160 try {
2161 mConnector.execute("asec", "fixperms", id, gid, filename);
2162 /*
2163 * Fix permissions does a remount, so no need to update
2164 * mAsecMountSet
2165 */
2166 } catch (NativeDaemonConnectorException e) {
2167 rc = StorageResultCode.OperationFailedInternalError;
2168 }
2169 return rc;
2170 }
2171
San Mehatd9709982010-02-18 11:43:03 -08002172 public int destroySecureContainer(String id, boolean force) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002173 enforcePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08002174 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08002175 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08002176
Kenny Rootaa485402010-09-14 14:49:41 -07002177 /*
2178 * Force a GC to make sure AssetManagers in other threads of the
2179 * system_server are cleaned up. We have to do this since AssetManager
2180 * instances are kept as a WeakReference and it's possible we have files
2181 * open on the external storage.
2182 */
2183 Runtime.getRuntime().gc();
2184
San Mehatb1043402010-02-05 08:26:50 -08002185 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08002186 try {
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002187 final Command cmd = new Command("asec", "destroy", id);
2188 if (force) {
2189 cmd.appendArg("force");
2190 }
2191 mConnector.execute(cmd);
San Mehat4270e1e2010-01-29 05:32:19 -08002192 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08002193 int code = e.getCode();
2194 if (code == VoldResponseCode.OpFailedStorageBusy) {
2195 rc = StorageResultCode.OperationFailedStorageBusy;
2196 } else {
2197 rc = StorageResultCode.OperationFailedInternalError;
2198 }
San Mehat02735bc2010-01-26 15:18:08 -08002199 }
San Mehata181b212010-02-11 06:50:20 -08002200
2201 if (rc == StorageResultCode.OperationSucceeded) {
2202 synchronized (mAsecMountSet) {
2203 if (mAsecMountSet.contains(id)) {
2204 mAsecMountSet.remove(id);
2205 }
2206 }
2207 }
2208
San Mehat4270e1e2010-01-29 05:32:19 -08002209 return rc;
San Mehat36972292010-01-06 11:06:32 -08002210 }
Jason parks9ed98bc2011-01-17 09:58:35 -06002211
Jeff Sharkey941a8ba2014-08-20 16:26:32 -07002212 public int mountSecureContainer(String id, String key, int ownerUid, boolean readOnly) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002213 enforcePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08002214 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08002215 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08002216
San Mehata181b212010-02-11 06:50:20 -08002217 synchronized (mAsecMountSet) {
2218 if (mAsecMountSet.contains(id)) {
2219 return StorageResultCode.OperationFailedStorageMounted;
2220 }
2221 }
2222
San Mehatb1043402010-02-05 08:26:50 -08002223 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08002224 try {
Jeff Sharkey941a8ba2014-08-20 16:26:32 -07002225 mConnector.execute("asec", "mount", id, new SensitiveArg(key), ownerUid,
2226 readOnly ? "ro" : "rw");
San Mehat4270e1e2010-01-29 05:32:19 -08002227 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07002228 int code = e.getCode();
2229 if (code != VoldResponseCode.OpFailedStorageBusy) {
2230 rc = StorageResultCode.OperationFailedInternalError;
2231 }
San Mehat02735bc2010-01-26 15:18:08 -08002232 }
San Mehat6cdd9c02010-02-09 14:45:20 -08002233
2234 if (rc == StorageResultCode.OperationSucceeded) {
2235 synchronized (mAsecMountSet) {
2236 mAsecMountSet.add(id);
2237 }
2238 }
San Mehat4270e1e2010-01-29 05:32:19 -08002239 return rc;
San Mehat36972292010-01-06 11:06:32 -08002240 }
2241
San Mehatd9709982010-02-18 11:43:03 -08002242 public int unmountSecureContainer(String id, boolean force) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002243 enforcePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08002244 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08002245 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08002246
San Mehat6cdd9c02010-02-09 14:45:20 -08002247 synchronized (mAsecMountSet) {
2248 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08002249 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08002250 }
2251 }
2252
Kenny Rootaa485402010-09-14 14:49:41 -07002253 /*
2254 * Force a GC to make sure AssetManagers in other threads of the
2255 * system_server are cleaned up. We have to do this since AssetManager
2256 * instances are kept as a WeakReference and it's possible we have files
2257 * open on the external storage.
2258 */
2259 Runtime.getRuntime().gc();
2260
San Mehatb1043402010-02-05 08:26:50 -08002261 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08002262 try {
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002263 final Command cmd = new Command("asec", "unmount", id);
2264 if (force) {
2265 cmd.appendArg("force");
2266 }
2267 mConnector.execute(cmd);
San Mehat4270e1e2010-01-29 05:32:19 -08002268 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08002269 int code = e.getCode();
2270 if (code == VoldResponseCode.OpFailedStorageBusy) {
2271 rc = StorageResultCode.OperationFailedStorageBusy;
2272 } else {
2273 rc = StorageResultCode.OperationFailedInternalError;
2274 }
San Mehat02735bc2010-01-26 15:18:08 -08002275 }
San Mehat6cdd9c02010-02-09 14:45:20 -08002276
2277 if (rc == StorageResultCode.OperationSucceeded) {
2278 synchronized (mAsecMountSet) {
2279 mAsecMountSet.remove(id);
2280 }
2281 }
San Mehat4270e1e2010-01-29 05:32:19 -08002282 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08002283 }
2284
San Mehat6cdd9c02010-02-09 14:45:20 -08002285 public boolean isSecureContainerMounted(String id) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002286 enforcePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat6cdd9c02010-02-09 14:45:20 -08002287 waitForReady();
2288 warnOnNotMounted();
2289
2290 synchronized (mAsecMountSet) {
2291 return mAsecMountSet.contains(id);
2292 }
2293 }
2294
San Mehat4270e1e2010-01-29 05:32:19 -08002295 public int renameSecureContainer(String oldId, String newId) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002296 enforcePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08002297 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08002298 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08002299
San Mehata181b212010-02-11 06:50:20 -08002300 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08002301 /*
Jason parks9ed98bc2011-01-17 09:58:35 -06002302 * Because a mounted container has active internal state which cannot be
San Mehat85451ee2010-02-24 08:54:18 -08002303 * changed while active, we must ensure both ids are not currently mounted.
2304 */
2305 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08002306 return StorageResultCode.OperationFailedStorageMounted;
2307 }
2308 }
2309
San Mehatb1043402010-02-05 08:26:50 -08002310 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08002311 try {
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002312 mConnector.execute("asec", "rename", oldId, newId);
San Mehat4270e1e2010-01-29 05:32:19 -08002313 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08002314 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08002315 }
San Mehata181b212010-02-11 06:50:20 -08002316
San Mehat4270e1e2010-01-29 05:32:19 -08002317 return rc;
San Mehat45f61042010-01-23 08:12:43 -08002318 }
2319
San Mehat4270e1e2010-01-29 05:32:19 -08002320 public String getSecureContainerPath(String id) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002321 enforcePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08002322 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08002323 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08002324
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002325 final NativeDaemonEvent event;
San Mehat2d66cef2010-03-23 11:12:52 -07002326 try {
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002327 event = mConnector.execute("asec", "path", id);
2328 event.checkCode(VoldResponseCode.AsecPathResult);
2329 return event.getMessage();
San Mehat2d66cef2010-03-23 11:12:52 -07002330 } catch (NativeDaemonConnectorException e) {
2331 int code = e.getCode();
2332 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Fredrik Helmera20c8ef2011-02-09 16:16:10 +01002333 Slog.i(TAG, String.format("Container '%s' not found", id));
2334 return null;
San Mehat22dd86e2010-01-12 12:21:18 -08002335 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07002336 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08002337 }
2338 }
San Mehat22dd86e2010-01-12 12:21:18 -08002339 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07002340
Dianne Hackborn292f8bc2011-06-27 16:27:41 -07002341 public String getSecureContainerFilesystemPath(String id) {
Jeff Sharkey48877892015-03-18 11:27:19 -07002342 enforcePermission(android.Manifest.permission.ASEC_ACCESS);
Dianne Hackborn292f8bc2011-06-27 16:27:41 -07002343 waitForReady();
2344 warnOnNotMounted();
2345
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002346 final NativeDaemonEvent event;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -07002347 try {
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002348 event = mConnector.execute("asec", "fspath", id);
2349 event.checkCode(VoldResponseCode.AsecPathResult);
2350 return event.getMessage();
Dianne Hackborn292f8bc2011-06-27 16:27:41 -07002351 } catch (NativeDaemonConnectorException e) {
2352 int code = e.getCode();
2353 if (code == VoldResponseCode.OpFailedStorageNotFound) {
2354 Slog.i(TAG, String.format("Container '%s' not found", id));
2355 return null;
2356 } else {
2357 throw new IllegalStateException(String.format("Unexpected response code %d", code));
2358 }
2359 }
2360 }
2361
Jeff Sharkey48877892015-03-18 11:27:19 -07002362 @Override
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07002363 public void finishMediaUpdate() {
Jeff Sharkey9527b222015-06-24 15:24:48 -07002364 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
Rubin Xucd7a0142015-04-17 23:45:27 +01002365 throw new SecurityException("no permission to call finishMediaUpdate()");
2366 }
Jeff Sharkey48877892015-03-18 11:27:19 -07002367 if (mUnmountSignal != null) {
2368 mUnmountSignal.countDown();
2369 } else {
2370 Slog.w(TAG, "Odd, nobody asked to unmount?");
2371 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07002372 }
Kenny Root02c87302010-07-01 08:10:18 -07002373
Kenny Roota02b8b02010-08-05 16:14:17 -07002374 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
2375 if (callerUid == android.os.Process.SYSTEM_UID) {
2376 return true;
2377 }
2378
Kenny Root02c87302010-07-01 08:10:18 -07002379 if (packageName == null) {
2380 return false;
2381 }
2382
Jeff Sharkeycd654482016-01-08 17:42:11 -07002383 final int packageUid = mPms.getPackageUid(packageName,
2384 PackageManager.MATCH_DEBUG_TRIAGED_MISSING, UserHandle.getUserId(callerUid));
Kenny Root02c87302010-07-01 08:10:18 -07002385
2386 if (DEBUG_OBB) {
2387 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
2388 packageUid + ", callerUid = " + callerUid);
2389 }
2390
2391 return callerUid == packageUid;
2392 }
2393
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002394 public String getMountedObbPath(String rawPath) {
2395 Preconditions.checkNotNull(rawPath, "rawPath cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002396
Kenny Root02c87302010-07-01 08:10:18 -07002397 waitForReady();
2398 warnOnNotMounted();
2399
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002400 final ObbState state;
Rubin Xucd7a0142015-04-17 23:45:27 +01002401 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002402 state = mObbPathToStateMap.get(rawPath);
2403 }
2404 if (state == null) {
2405 Slog.w(TAG, "Failed to find OBB mounted at " + rawPath);
2406 return null;
2407 }
2408
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002409 final NativeDaemonEvent event;
Kenny Root02c87302010-07-01 08:10:18 -07002410 try {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07002411 event = mConnector.execute("obb", "path", state.canonicalPath);
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002412 event.checkCode(VoldResponseCode.AsecPathResult);
2413 return event.getMessage();
Kenny Root02c87302010-07-01 08:10:18 -07002414 } catch (NativeDaemonConnectorException e) {
2415 int code = e.getCode();
2416 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07002417 return null;
Kenny Root02c87302010-07-01 08:10:18 -07002418 } else {
2419 throw new IllegalStateException(String.format("Unexpected response code %d", code));
2420 }
2421 }
2422 }
2423
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002424 @Override
2425 public boolean isObbMounted(String rawPath) {
2426 Preconditions.checkNotNull(rawPath, "rawPath cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002427 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002428 return mObbPathToStateMap.containsKey(rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002429 }
Kenny Root02c87302010-07-01 08:10:18 -07002430 }
2431
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002432 @Override
2433 public void mountObb(
2434 String rawPath, String canonicalPath, String key, IObbActionListener token, int nonce) {
2435 Preconditions.checkNotNull(rawPath, "rawPath cannot be null");
2436 Preconditions.checkNotNull(canonicalPath, "canonicalPath cannot be null");
2437 Preconditions.checkNotNull(token, "token cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002438
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002439 final int callingUid = Binder.getCallingUid();
2440 final ObbState obbState = new ObbState(rawPath, canonicalPath, callingUid, token, nonce);
2441 final ObbAction action = new MountObbAction(obbState, key, callingUid);
Kenny Roota02b8b02010-08-05 16:14:17 -07002442 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
2443
2444 if (DEBUG_OBB)
2445 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07002446 }
2447
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002448 @Override
2449 public void unmountObb(String rawPath, boolean force, IObbActionListener token, int nonce) {
2450 Preconditions.checkNotNull(rawPath, "rawPath cannot be null");
2451
2452 final ObbState existingState;
Rubin Xucd7a0142015-04-17 23:45:27 +01002453 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002454 existingState = mObbPathToStateMap.get(rawPath);
Kenny Rootf1121dc2010-09-29 07:30:53 -07002455 }
2456
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002457 if (existingState != null) {
2458 // TODO: separate state object from request data
2459 final int callingUid = Binder.getCallingUid();
2460 final ObbState newState = new ObbState(
2461 rawPath, existingState.canonicalPath, callingUid, token, nonce);
2462 final ObbAction action = new UnmountObbAction(newState, force);
2463 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07002464
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07002465 if (DEBUG_OBB)
2466 Slog.i(TAG, "Send to OBB handler: " + action.toString());
2467 } else {
2468 Slog.w(TAG, "Unknown OBB mount at " + rawPath);
2469 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002470 }
2471
Ben Komalo444eca22011-09-01 15:17:44 -07002472 @Override
2473 public int getEncryptionState() {
2474 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2475 "no permission to access the crypt keeper");
2476
2477 waitForReady();
2478
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002479 final NativeDaemonEvent event;
Ben Komalo444eca22011-09-01 15:17:44 -07002480 try {
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07002481 event = mCryptConnector.execute("cryptfs", "cryptocomplete");
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002482 return Integer.parseInt(event.getMessage());
Ben Komalo444eca22011-09-01 15:17:44 -07002483 } catch (NumberFormatException e) {
2484 // Bad result - unexpected.
2485 Slog.w(TAG, "Unable to parse result from cryptfs cryptocomplete");
2486 return ENCRYPTION_STATE_ERROR_UNKNOWN;
2487 } catch (NativeDaemonConnectorException e) {
2488 // Something bad happened.
2489 Slog.w(TAG, "Error in communicating with cryptfs in validating");
2490 return ENCRYPTION_STATE_ERROR_UNKNOWN;
2491 }
2492 }
2493
2494 @Override
Jason parks5af0b912010-11-29 09:05:25 -06002495 public int decryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06002496 if (TextUtils.isEmpty(password)) {
2497 throw new IllegalArgumentException("password cannot be empty");
Jason parks5af0b912010-11-29 09:05:25 -06002498 }
2499
Jason parks8888c592011-01-20 22:46:41 -06002500 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2501 "no permission to access the crypt keeper");
Jason parks5af0b912010-11-29 09:05:25 -06002502
2503 waitForReady();
2504
2505 if (DEBUG_EVENTS) {
2506 Slog.i(TAG, "decrypting storage...");
2507 }
2508
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002509 final NativeDaemonEvent event;
Jason parks5af0b912010-11-29 09:05:25 -06002510 try {
Paul Lawrence05487612015-06-09 13:35:38 -07002511 event = mCryptConnector.execute("cryptfs", "checkpw", new SensitiveArg(password));
Jason parks9ed98bc2011-01-17 09:58:35 -06002512
Fredrik Roubertda6aedf2011-12-20 17:34:43 +01002513 final int code = Integer.parseInt(event.getMessage());
Jason parks9ed98bc2011-01-17 09:58:35 -06002514 if (code == 0) {
2515 // Decrypt was successful. Post a delayed message before restarting in order
2516 // to let the UI to clear itself
2517 mHandler.postDelayed(new Runnable() {
2518 public void run() {
Jeff Sharkey31c6e482011-11-18 17:09:01 -08002519 try {
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07002520 mCryptConnector.execute("cryptfs", "restart");
Jeff Sharkey31c6e482011-11-18 17:09:01 -08002521 } catch (NativeDaemonConnectorException e) {
2522 Slog.e(TAG, "problem executing in background", e);
2523 }
Jason parks9ed98bc2011-01-17 09:58:35 -06002524 }
Jason parksf7b3cd42011-01-27 09:28:25 -06002525 }, 1000); // 1 second
Jason parks9ed98bc2011-01-17 09:58:35 -06002526 }
2527
2528 return code;
Jason parks5af0b912010-11-29 09:05:25 -06002529 } catch (NativeDaemonConnectorException e) {
2530 // Decryption failed
2531 return e.getCode();
2532 }
Jason parks5af0b912010-11-29 09:05:25 -06002533 }
2534
Paul Lawrence46791e72014-04-03 09:10:26 -07002535 public int encryptStorage(int type, String password) {
2536 if (TextUtils.isEmpty(password) && type != StorageManager.CRYPT_TYPE_DEFAULT) {
Jason parksf7b3cd42011-01-27 09:28:25 -06002537 throw new IllegalArgumentException("password cannot be empty");
Jason parks56aa5322011-01-07 09:01:15 -06002538 }
2539
Jason parks8888c592011-01-20 22:46:41 -06002540 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2541 "no permission to access the crypt keeper");
Jason parks56aa5322011-01-07 09:01:15 -06002542
2543 waitForReady();
2544
2545 if (DEBUG_EVENTS) {
Jason parks8888c592011-01-20 22:46:41 -06002546 Slog.i(TAG, "encrypting storage...");
Jason parks56aa5322011-01-07 09:01:15 -06002547 }
2548
2549 try {
Paul Lawrence5096d9e2015-09-09 13:05:45 -07002550 if (type == StorageManager.CRYPT_TYPE_DEFAULT) {
2551 mCryptConnector.execute("cryptfs", "enablecrypto", "inplace",
2552 CRYPTO_TYPES[type]);
2553 } else {
2554 mCryptConnector.execute("cryptfs", "enablecrypto", "inplace",
2555 CRYPTO_TYPES[type], new SensitiveArg(password));
2556 }
Jason parks56aa5322011-01-07 09:01:15 -06002557 } catch (NativeDaemonConnectorException e) {
2558 // Encryption failed
2559 return e.getCode();
2560 }
2561
2562 return 0;
2563 }
2564
Paul Lawrence8e397362014-01-27 15:22:30 -08002565 /** Set the password for encrypting the master key.
2566 * @param type One of the CRYPTO_TYPE_XXX consts defined in StorageManager.
2567 * @param password The password to set.
2568 */
2569 public int changeEncryptionPassword(int type, String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06002570 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2571 "no permission to access the crypt keeper");
2572
2573 waitForReady();
2574
2575 if (DEBUG_EVENTS) {
2576 Slog.i(TAG, "changing encryption password...");
2577 }
2578
2579 try {
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07002580 NativeDaemonEvent event = mCryptConnector.execute("cryptfs", "changepw", CRYPTO_TYPES[type],
Paul Lawrence05487612015-06-09 13:35:38 -07002581 new SensitiveArg(password));
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002582 return Integer.parseInt(event.getMessage());
Jason parksf7b3cd42011-01-27 09:28:25 -06002583 } catch (NativeDaemonConnectorException e) {
2584 // Encryption failed
2585 return e.getCode();
2586 }
2587 }
2588
Christopher Tate32418be2011-10-10 13:51:12 -07002589 /**
2590 * Validate a user-supplied password string with cryptfs
2591 */
2592 @Override
2593 public int verifyEncryptionPassword(String password) throws RemoteException {
2594 // Only the system process is permitted to validate passwords
2595 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
2596 throw new SecurityException("no permission to access the crypt keeper");
2597 }
2598
2599 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
2600 "no permission to access the crypt keeper");
2601
2602 if (TextUtils.isEmpty(password)) {
2603 throw new IllegalArgumentException("password cannot be empty");
2604 }
2605
2606 waitForReady();
2607
2608 if (DEBUG_EVENTS) {
2609 Slog.i(TAG, "validating encryption password...");
2610 }
2611
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002612 final NativeDaemonEvent event;
Christopher Tate32418be2011-10-10 13:51:12 -07002613 try {
Paul Lawrence05487612015-06-09 13:35:38 -07002614 event = mCryptConnector.execute("cryptfs", "verifypw", new SensitiveArg(password));
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08002615 Slog.i(TAG, "cryptfs verifypw => " + event.getMessage());
2616 return Integer.parseInt(event.getMessage());
Christopher Tate32418be2011-10-10 13:51:12 -07002617 } catch (NativeDaemonConnectorException e) {
2618 // Encryption failed
2619 return e.getCode();
2620 }
2621 }
2622
Paul Lawrence8e397362014-01-27 15:22:30 -08002623 /**
2624 * Get the type of encryption used to encrypt the master key.
2625 * @return The type, one of the CRYPT_TYPE_XXX consts from StorageManager.
2626 */
2627 @Override
Svetoslav16e4a1a2014-09-29 18:16:20 -07002628 public int getPasswordType() {
Paul Lawrence8e397362014-01-27 15:22:30 -08002629
2630 waitForReady();
2631
2632 final NativeDaemonEvent event;
2633 try {
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07002634 event = mCryptConnector.execute("cryptfs", "getpwtype");
Paul Lawrence8e397362014-01-27 15:22:30 -08002635 for (int i = 0; i < CRYPTO_TYPES.length; ++i) {
2636 if (CRYPTO_TYPES[i].equals(event.getMessage()))
2637 return i;
2638 }
2639
2640 throw new IllegalStateException("unexpected return from cryptfs");
2641 } catch (NativeDaemonConnectorException e) {
2642 throw e.rethrowAsParcelableException();
2643 }
2644 }
2645
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002646 /**
2647 * Set a field in the crypto header.
2648 * @param field field to set
2649 * @param contents contents to set in field
2650 */
2651 @Override
2652 public void setField(String field, String contents) throws RemoteException {
2653
2654 waitForReady();
2655
2656 final NativeDaemonEvent event;
2657 try {
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07002658 event = mCryptConnector.execute("cryptfs", "setfield", field, contents);
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002659 } catch (NativeDaemonConnectorException e) {
2660 throw e.rethrowAsParcelableException();
2661 }
2662 }
2663
2664 /**
2665 * Gets a field from the crypto header.
2666 * @param field field to get
2667 * @return contents of field
2668 */
2669 @Override
2670 public String getField(String field) throws RemoteException {
2671
2672 waitForReady();
2673
2674 final NativeDaemonEvent event;
2675 try {
2676 final String[] contents = NativeDaemonEvent.filterMessageList(
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07002677 mCryptConnector.executeForList("cryptfs", "getfield", field),
Paul Lawrencee51dcf92014-03-18 10:56:00 -07002678 VoldResponseCode.CryptfsGetfieldResult);
2679 String result = new String();
2680 for (String content : contents) {
2681 result += content;
2682 }
2683 return result;
2684 } catch (NativeDaemonConnectorException e) {
2685 throw e.rethrowAsParcelableException();
2686 }
2687 }
2688
Paul Lawrence3806d9c2015-10-29 10:30:46 -07002689 /**
2690 * Is userdata convertible to file based encryption?
2691 * @return non zero for convertible
2692 */
2693 @Override
2694 public boolean isConvertibleToFBE() throws RemoteException {
2695
2696 waitForReady();
2697
2698 final NativeDaemonEvent event;
2699 try {
2700 event = mCryptConnector.execute("cryptfs", "isConvertibleToFBE");
2701 return Integer.parseInt(event.getMessage()) != 0;
2702 } catch (NativeDaemonConnectorException e) {
2703 throw e.rethrowAsParcelableException();
2704 }
2705 }
2706
Jeff Sharkeyb049e212012-09-07 23:16:01 -07002707 @Override
Paul Lawrence945490c2014-03-27 16:37:28 +00002708 public String getPassword() throws RemoteException {
Rubin Xucd7a0142015-04-17 23:45:27 +01002709 mContext.enforceCallingOrSelfPermission(Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE,
2710 "only keyguard can retrieve password");
Paul Lawrence945490c2014-03-27 16:37:28 +00002711 if (!isReady()) {
2712 return new String();
2713 }
2714
2715 final NativeDaemonEvent event;
2716 try {
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07002717 event = mCryptConnector.execute("cryptfs", "getpw");
Paul Lawrence24063b52015-01-06 13:11:23 -08002718 if ("-1".equals(event.getMessage())) {
2719 // -1 equals no password
2720 return null;
2721 }
Paul Lawrence05487612015-06-09 13:35:38 -07002722 return event.getMessage();
Paul Lawrence945490c2014-03-27 16:37:28 +00002723 } catch (NativeDaemonConnectorException e) {
2724 throw e.rethrowAsParcelableException();
Paul Lawrence24063b52015-01-06 13:11:23 -08002725 } catch (IllegalArgumentException e) {
2726 Slog.e(TAG, "Invalid response to getPassword");
2727 return null;
Paul Lawrence945490c2014-03-27 16:37:28 +00002728 }
2729 }
2730
2731 @Override
2732 public void clearPassword() throws RemoteException {
2733 if (!isReady()) {
2734 return;
2735 }
2736
2737 final NativeDaemonEvent event;
2738 try {
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07002739 event = mCryptConnector.execute("cryptfs", "clearpw");
Paul Lawrence945490c2014-03-27 16:37:28 +00002740 } catch (NativeDaemonConnectorException e) {
2741 throw e.rethrowAsParcelableException();
2742 }
2743 }
2744
2745 @Override
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01002746 public void createUserKey(int userId, int serialNumber, boolean ephemeral) {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002747 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Paul Crowleybcf48ed2015-04-22 13:36:59 +01002748 waitForReady();
2749
Paul Crowleybcf48ed2015-04-22 13:36:59 +01002750 try {
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01002751 mCryptConnector.execute("cryptfs", "create_user_key", userId, serialNumber,
2752 ephemeral ? 1 : 0);
Paul Crowleybcf48ed2015-04-22 13:36:59 +01002753 } catch (NativeDaemonConnectorException e) {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002754 throw e.rethrowAsParcelableException();
Paul Crowleybcf48ed2015-04-22 13:36:59 +01002755 }
2756 }
2757
Paul Crowley7ec733f2015-05-19 12:42:00 +01002758 @Override
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002759 public void destroyUserKey(int userId) {
2760 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
Paul Crowley7ec733f2015-05-19 12:42:00 +01002761 waitForReady();
2762
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002763 try {
2764 mCryptConnector.execute("cryptfs", "destroy_user_key", userId);
2765 } catch (NativeDaemonConnectorException e) {
2766 throw e.rethrowAsParcelableException();
2767 }
2768 }
2769
Paul Crowleyfaeb3eb2016-02-08 15:58:29 +00002770 private SensitiveArg encodeBytes(byte[] bytes) {
2771 if (ArrayUtils.isEmpty(bytes)) {
2772 return new SensitiveArg("!");
2773 } else {
2774 return new SensitiveArg(HexDump.toHexString(bytes));
2775 }
2776 }
2777
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002778 @Override
Paul Crowleyfaeb3eb2016-02-08 15:58:29 +00002779 public void changeUserKey(int userId, int serialNumber,
2780 byte[] token, byte[] oldSecret, byte[] newSecret) {
2781 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
2782 waitForReady();
2783
2784 try {
2785 mCryptConnector.execute("cryptfs", "change_user_key", userId, serialNumber,
2786 encodeBytes(token), encodeBytes(oldSecret), encodeBytes(newSecret));
2787 } catch (NativeDaemonConnectorException e) {
2788 throw e.rethrowAsParcelableException();
2789 }
2790 }
2791
2792 @Override
2793 public void unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret) {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002794 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
2795 waitForReady();
2796
Jeff Sharkeyb9fe5372015-12-03 15:23:08 -07002797 // When a user has secure lock screen, require a challenge token to
2798 // actually unlock. This check is mostly in place for emulation mode.
2799 if (mLockPatternUtils.isSecure(userId) && ArrayUtils.isEmpty(token)) {
2800 throw new IllegalStateException("Token required to unlock secure user " + userId);
2801 }
2802
Paul Crowley7ec733f2015-05-19 12:42:00 +01002803 try {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002804 mCryptConnector.execute("cryptfs", "unlock_user_key", userId, serialNumber,
Paul Crowleyfaeb3eb2016-02-08 15:58:29 +00002805 encodeBytes(token), encodeBytes(secret));
Paul Crowley7ec733f2015-05-19 12:42:00 +01002806 } catch (NativeDaemonConnectorException e) {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002807 throw e.rethrowAsParcelableException();
2808 }
2809
2810 synchronized (mLock) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07002811 mLocalUnlockedUsers = ArrayUtils.appendInt(mLocalUnlockedUsers, userId);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002812 }
2813 }
2814
2815 @Override
2816 public void lockUserKey(int userId) {
2817 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
2818 waitForReady();
2819
2820 try {
2821 mCryptConnector.execute("cryptfs", "lock_user_key", userId);
2822 } catch (NativeDaemonConnectorException e) {
2823 throw e.rethrowAsParcelableException();
2824 }
2825
2826 synchronized (mLock) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07002827 mLocalUnlockedUsers = ArrayUtils.removeInt(mLocalUnlockedUsers, userId);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002828 }
2829 }
2830
2831 @Override
2832 public boolean isUserKeyUnlocked(int userId) {
Paul Lawrence20be5d62016-02-26 13:51:17 -08002833 if (StorageManager.isFileEncryptedNativeOrEmulated()) {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002834 synchronized (mLock) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07002835 return ArrayUtils.contains(mLocalUnlockedUsers, userId);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002836 }
2837 } else {
2838 return true;
2839 }
2840 }
2841
2842 @Override
Jeff Sharkey47f71082016-02-01 17:03:54 -07002843 public void prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags) {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002844 enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
2845 waitForReady();
2846
2847 try {
2848 mCryptConnector.execute("cryptfs", "prepare_user_storage", escapeNull(volumeUuid),
Jeff Sharkey47f71082016-02-01 17:03:54 -07002849 userId, serialNumber, flags);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002850 } catch (NativeDaemonConnectorException e) {
2851 throw e.rethrowAsParcelableException();
Paul Crowley7ec733f2015-05-19 12:42:00 +01002852 }
2853 }
2854
Paul Crowleybcf48ed2015-04-22 13:36:59 +01002855 @Override
Daichi Hirono91e3b502015-12-16 09:24:16 +09002856 public ParcelFileDescriptor mountAppFuse(final String name) throws RemoteException {
Daichi Hironobee50c02015-12-14 11:00:54 +09002857 try {
Daichi Hirono91e3b502015-12-16 09:24:16 +09002858 final int uid = Binder.getCallingUid();
Daichi Hironofd7d57e2016-01-29 14:30:58 +09002859 final int pid = Binder.getCallingPid();
Daichi Hironobee50c02015-12-14 11:00:54 +09002860 final NativeDaemonEvent event =
Daichi Hironofd7d57e2016-01-29 14:30:58 +09002861 mConnector.execute("appfuse", "mount", uid, pid, name);
Daichi Hironobee50c02015-12-14 11:00:54 +09002862 if (event.getFileDescriptors() == null) {
Daichi Hirono91e3b502015-12-16 09:24:16 +09002863 throw new RemoteException("AppFuse FD from vold is null.");
Daichi Hironobee50c02015-12-14 11:00:54 +09002864 }
Daichi Hirono91e3b502015-12-16 09:24:16 +09002865 return ParcelFileDescriptor.fromFd(
2866 event.getFileDescriptors()[0],
2867 mHandler,
2868 new ParcelFileDescriptor.OnCloseListener() {
2869 @Override
2870 public void onClose(IOException e) {
2871 try {
2872 final NativeDaemonEvent event = mConnector.execute(
Daichi Hironofd7d57e2016-01-29 14:30:58 +09002873 "appfuse", "unmount", uid, pid, name);
Daichi Hirono91e3b502015-12-16 09:24:16 +09002874 } catch (NativeDaemonConnectorException unmountException) {
2875 Log.e(TAG, "Failed to unmount appfuse.");
2876 }
2877 }
2878 });
Daichi Hironobee50c02015-12-14 11:00:54 +09002879 } catch (NativeDaemonConnectorException e) {
2880 throw e.rethrowAsParcelableException();
Daichi Hirono91e3b502015-12-16 09:24:16 +09002881 } catch (IOException e) {
2882 throw new RemoteException(e.getMessage());
Daichi Hironobee50c02015-12-14 11:00:54 +09002883 }
Daichi Hirono9e8d9e22015-11-13 14:37:00 +09002884 }
2885
2886 @Override
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002887 public int mkdirs(String callingPkg, String appPath) {
2888 final int userId = UserHandle.getUserId(Binder.getCallingUid());
2889 final UserEnvironment userEnv = new UserEnvironment(userId);
2890
2891 // Validate that reported package name belongs to caller
2892 final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(
2893 Context.APP_OPS_SERVICE);
2894 appOps.checkPackage(Binder.getCallingUid(), callingPkg);
2895
Jeff Sharkey48877892015-03-18 11:27:19 -07002896 File appFile = null;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002897 try {
Jeff Sharkey48877892015-03-18 11:27:19 -07002898 appFile = new File(appPath).getCanonicalFile();
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002899 } catch (IOException e) {
2900 Slog.e(TAG, "Failed to resolve " + appPath + ": " + e);
2901 return -1;
2902 }
2903
2904 // Try translating the app path into a vold path, but require that it
2905 // belong to the calling package.
Jeff Sharkey48877892015-03-18 11:27:19 -07002906 if (FileUtils.contains(userEnv.buildExternalStorageAppDataDirs(callingPkg), appFile) ||
2907 FileUtils.contains(userEnv.buildExternalStorageAppObbDirs(callingPkg), appFile) ||
2908 FileUtils.contains(userEnv.buildExternalStorageAppMediaDirs(callingPkg), appFile)) {
2909 appPath = appFile.getAbsolutePath();
2910 if (!appPath.endsWith("/")) {
2911 appPath = appPath + "/";
2912 }
2913
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002914 try {
Jeff Sharkey48877892015-03-18 11:27:19 -07002915 mConnector.execute("volume", "mkdirs", appPath);
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002916 return 0;
2917 } catch (NativeDaemonConnectorException e) {
2918 return e.getCode();
2919 }
2920 }
2921
Jeff Sharkey48877892015-03-18 11:27:19 -07002922 throw new SecurityException("Invalid mkdirs path: " + appFile);
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002923 }
2924
2925 @Override
Jeff Sharkey46349872015-07-28 10:49:47 -07002926 public StorageVolume[] getVolumeList(int uid, String packageName, int flags) {
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07002927 final int userId = UserHandle.getUserId(uid);
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06002928
Jeff Sharkey46349872015-07-28 10:49:47 -07002929 final boolean forWrite = (flags & StorageManager.FLAG_FOR_WRITE) != 0;
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06002930 final boolean realState = (flags & StorageManager.FLAG_REAL_STATE) != 0;
2931 final boolean includeInvisible = (flags & StorageManager.FLAG_INCLUDE_INVISIBLE) != 0;
Jeff Sharkey46349872015-07-28 10:49:47 -07002932
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06002933 final boolean userKeyUnlocked;
2934 final boolean storagePermission;
2935 final long token = Binder.clearCallingIdentity();
Svetoslav38c3dbb2015-07-14 11:27:06 -07002936 try {
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06002937 userKeyUnlocked = isUserKeyUnlocked(userId);
2938 storagePermission = mMountServiceInternal.hasExternalStorage(uid, packageName);
Svetoslav38c3dbb2015-07-14 11:27:06 -07002939 } finally {
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06002940 Binder.restoreCallingIdentity(token);
Svetoslav38c3dbb2015-07-14 11:27:06 -07002941 }
Svet Ganov6ee871e2015-07-10 14:29:33 -07002942
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06002943 boolean foundPrimary = false;
2944
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07002945 final ArrayList<StorageVolume> res = new ArrayList<>();
Jeff Sharkey48877892015-03-18 11:27:19 -07002946 synchronized (mLock) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07002947 for (int i = 0; i < mVolumes.size(); i++) {
2948 final VolumeInfo vol = mVolumes.valueAt(i);
Jeff Sharkeyc02bfae2016-03-27 15:06:53 -06002949 switch (vol.getType()) {
2950 case VolumeInfo.TYPE_PUBLIC:
2951 case VolumeInfo.TYPE_EMULATED:
2952 break;
2953 default:
2954 continue;
2955 }
2956
2957 boolean match = false;
2958 if (forWrite) {
2959 match = vol.isVisibleForWrite(userId);
2960 } else {
2961 match = vol.isVisibleForRead(userId) || includeInvisible;
2962 }
2963 if (!match) continue;
2964
2965 boolean reportUnmounted = false;
2966 if ((vol.getType() == VolumeInfo.TYPE_EMULATED) && !userKeyUnlocked) {
2967 reportUnmounted = true;
2968 } else if (!storagePermission && !realState) {
2969 reportUnmounted = true;
2970 }
2971
2972 final StorageVolume userVol = vol.buildStorageVolume(mContext, userId,
2973 reportUnmounted);
2974 if (vol.isPrimary()) {
2975 res.add(0, userVol);
2976 foundPrimary = true;
2977 } else {
2978 res.add(userVol);
Jeff Sharkeyb049e212012-09-07 23:16:01 -07002979 }
Mike Lockwood8fa5f802011-03-24 08:12:30 -07002980 }
Mike Lockwood8fa5f802011-03-24 08:12:30 -07002981 }
Jeff Sharkey48877892015-03-18 11:27:19 -07002982
2983 if (!foundPrimary) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07002984 Log.w(TAG, "No primary storage defined yet; hacking together a stub");
Jeff Sharkey48877892015-03-18 11:27:19 -07002985
2986 final boolean primaryPhysical = SystemProperties.getBoolean(
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07002987 StorageManager.PROP_PRIMARY_PHYSICAL, false);
Jeff Sharkey48877892015-03-18 11:27:19 -07002988
2989 final String id = "stub_primary";
2990 final File path = Environment.getLegacyExternalStorageDirectory();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07002991 final String description = mContext.getString(android.R.string.unknownName);
Jeff Sharkey48877892015-03-18 11:27:19 -07002992 final boolean primary = true;
2993 final boolean removable = primaryPhysical;
2994 final boolean emulated = !primaryPhysical;
2995 final long mtpReserveSize = 0L;
2996 final boolean allowMassStorage = false;
2997 final long maxFileSize = 0L;
2998 final UserHandle owner = new UserHandle(userId);
2999 final String uuid = null;
Jeff Sharkey48877892015-03-18 11:27:19 -07003000 final String state = Environment.MEDIA_REMOVED;
3001
Jeff Sharkey5af1835d2015-07-07 17:26:59 -07003002 res.add(0, new StorageVolume(id, StorageVolume.STORAGE_ID_INVALID, path,
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07003003 description, primary, removable, emulated, mtpReserveSize,
3004 allowMassStorage, maxFileSize, owner, uuid, state));
Jeff Sharkey48877892015-03-18 11:27:19 -07003005 }
3006
3007 return res.toArray(new StorageVolume[res.size()]);
Mike Lockwood8fa5f802011-03-24 08:12:30 -07003008 }
3009
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07003010 @Override
3011 public DiskInfo[] getDisks() {
3012 synchronized (mLock) {
3013 final DiskInfo[] res = new DiskInfo[mDisks.size()];
3014 for (int i = 0; i < mDisks.size(); i++) {
3015 res[i] = mDisks.valueAt(i);
3016 }
3017 return res;
3018 }
3019 }
3020
3021 @Override
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07003022 public VolumeInfo[] getVolumes(int flags) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07003023 synchronized (mLock) {
3024 final VolumeInfo[] res = new VolumeInfo[mVolumes.size()];
3025 for (int i = 0; i < mVolumes.size(); i++) {
3026 res[i] = mVolumes.valueAt(i);
3027 }
3028 return res;
3029 }
3030 }
3031
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07003032 @Override
3033 public VolumeRecord[] getVolumeRecords(int flags) {
3034 synchronized (mLock) {
3035 final VolumeRecord[] res = new VolumeRecord[mRecords.size()];
3036 for (int i = 0; i < mRecords.size(); i++) {
3037 res[i] = mRecords.valueAt(i);
3038 }
3039 return res;
3040 }
3041 }
3042
Kenny Rootaf9d6672010-10-08 09:21:39 -07003043 private void addObbStateLocked(ObbState obbState) throws RemoteException {
3044 final IBinder binder = obbState.getBinder();
3045 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07003046
Kenny Rootaf9d6672010-10-08 09:21:39 -07003047 if (obbStates == null) {
3048 obbStates = new ArrayList<ObbState>();
3049 mObbMounts.put(binder, obbStates);
3050 } else {
3051 for (final ObbState o : obbStates) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003052 if (o.rawPath.equals(obbState.rawPath)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003053 throw new IllegalStateException("Attempt to add ObbState twice. "
3054 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07003055 }
3056 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003057 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07003058
3059 obbStates.add(obbState);
3060 try {
3061 obbState.link();
3062 } catch (RemoteException e) {
3063 /*
3064 * The binder died before we could link it, so clean up our state
3065 * and return failure.
3066 */
3067 obbStates.remove(obbState);
3068 if (obbStates.isEmpty()) {
3069 mObbMounts.remove(binder);
3070 }
3071
3072 // Rethrow the error so mountObb can get it
3073 throw e;
3074 }
3075
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003076 mObbPathToStateMap.put(obbState.rawPath, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07003077 }
3078
Kenny Rootaf9d6672010-10-08 09:21:39 -07003079 private void removeObbStateLocked(ObbState obbState) {
3080 final IBinder binder = obbState.getBinder();
3081 final List<ObbState> obbStates = mObbMounts.get(binder);
3082 if (obbStates != null) {
3083 if (obbStates.remove(obbState)) {
3084 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07003085 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07003086 if (obbStates.isEmpty()) {
3087 mObbMounts.remove(binder);
3088 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003089 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003090
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003091 mObbPathToStateMap.remove(obbState.rawPath);
Kenny Root38cf8862010-09-26 14:18:51 -07003092 }
3093
Kenny Roota02b8b02010-08-05 16:14:17 -07003094 private class ObbActionHandler extends Handler {
3095 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07003096 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07003097
3098 ObbActionHandler(Looper l) {
3099 super(l);
3100 }
3101
3102 @Override
3103 public void handleMessage(Message msg) {
3104 switch (msg.what) {
3105 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07003106 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07003107
3108 if (DEBUG_OBB)
3109 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
3110
3111 // If a bind was already initiated we don't really
3112 // need to do anything. The pending install
3113 // will be processed later on.
3114 if (!mBound) {
3115 // If this is the only one pending we might
3116 // have to bind to the service again.
3117 if (!connectToService()) {
3118 Slog.e(TAG, "Failed to bind to media container service");
3119 action.handleError();
3120 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07003121 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003122 }
Kenny Root735de3b2010-09-30 14:11:39 -07003123
Kenny Root735de3b2010-09-30 14:11:39 -07003124 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07003125 break;
3126 }
3127 case OBB_MCS_BOUND: {
3128 if (DEBUG_OBB)
3129 Slog.i(TAG, "OBB_MCS_BOUND");
3130 if (msg.obj != null) {
3131 mContainerService = (IMediaContainerService) msg.obj;
3132 }
3133 if (mContainerService == null) {
3134 // Something seriously wrong. Bail out
3135 Slog.e(TAG, "Cannot bind to media container service");
3136 for (ObbAction action : mActions) {
3137 // Indicate service bind error
3138 action.handleError();
3139 }
3140 mActions.clear();
3141 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07003142 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07003143 if (action != null) {
3144 action.execute(this);
3145 }
3146 } else {
3147 // Should never happen ideally.
3148 Slog.w(TAG, "Empty queue");
3149 }
3150 break;
3151 }
3152 case OBB_MCS_RECONNECT: {
3153 if (DEBUG_OBB)
3154 Slog.i(TAG, "OBB_MCS_RECONNECT");
3155 if (mActions.size() > 0) {
3156 if (mBound) {
3157 disconnectService();
3158 }
3159 if (!connectToService()) {
3160 Slog.e(TAG, "Failed to bind to media container service");
3161 for (ObbAction action : mActions) {
3162 // Indicate service bind error
3163 action.handleError();
3164 }
3165 mActions.clear();
3166 }
3167 }
3168 break;
3169 }
3170 case OBB_MCS_UNBIND: {
3171 if (DEBUG_OBB)
3172 Slog.i(TAG, "OBB_MCS_UNBIND");
3173
3174 // Delete pending install
3175 if (mActions.size() > 0) {
3176 mActions.remove(0);
3177 }
3178 if (mActions.size() == 0) {
3179 if (mBound) {
3180 disconnectService();
3181 }
3182 } else {
3183 // There are more pending requests in queue.
3184 // Just post MCS_BOUND message to trigger processing
3185 // of next pending install.
3186 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
3187 }
3188 break;
3189 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07003190 case OBB_FLUSH_MOUNT_STATE: {
3191 final String path = (String) msg.obj;
3192
3193 if (DEBUG_OBB)
3194 Slog.i(TAG, "Flushing all OBB state for path " + path);
3195
3196 synchronized (mObbMounts) {
3197 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
3198
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003199 final Iterator<ObbState> i = mObbPathToStateMap.values().iterator();
Kenny Rootaf9d6672010-10-08 09:21:39 -07003200 while (i.hasNext()) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003201 final ObbState state = i.next();
Kenny Rootaf9d6672010-10-08 09:21:39 -07003202
3203 /*
3204 * If this entry's source file is in the volume path
3205 * that got unmounted, remove it because it's no
3206 * longer valid.
3207 */
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003208 if (state.canonicalPath.startsWith(path)) {
3209 obbStatesToRemove.add(state);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003210 }
3211 }
3212
3213 for (final ObbState obbState : obbStatesToRemove) {
3214 if (DEBUG_OBB)
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003215 Slog.i(TAG, "Removing state for " + obbState.rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003216
3217 removeObbStateLocked(obbState);
3218
3219 try {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003220 obbState.token.onObbResult(obbState.rawPath, obbState.nonce,
Kenny Rootaf9d6672010-10-08 09:21:39 -07003221 OnObbStateChangeListener.UNMOUNTED);
3222 } catch (RemoteException e) {
3223 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003224 + obbState.rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003225 }
3226 }
3227 }
3228 break;
3229 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003230 }
3231 }
3232
3233 private boolean connectToService() {
3234 if (DEBUG_OBB)
3235 Slog.i(TAG, "Trying to bind to DefaultContainerService");
3236
3237 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
Jeff Sharkey6dce4962015-07-03 18:08:41 -07003238 if (mContext.bindServiceAsUser(service, mDefContainerConn, Context.BIND_AUTO_CREATE,
Xiaohui Chene4de5a02015-09-22 15:33:31 -07003239 UserHandle.SYSTEM)) {
Kenny Roota02b8b02010-08-05 16:14:17 -07003240 mBound = true;
3241 return true;
3242 }
3243 return false;
3244 }
3245
3246 private void disconnectService() {
3247 mContainerService = null;
3248 mBound = false;
3249 mContext.unbindService(mDefContainerConn);
3250 }
3251 }
3252
3253 abstract class ObbAction {
3254 private static final int MAX_RETRIES = 3;
3255 private int mRetries;
3256
3257 ObbState mObbState;
3258
3259 ObbAction(ObbState obbState) {
3260 mObbState = obbState;
3261 }
3262
3263 public void execute(ObbActionHandler handler) {
3264 try {
3265 if (DEBUG_OBB)
Ben Komalo444eca22011-09-01 15:17:44 -07003266 Slog.i(TAG, "Starting to execute action: " + toString());
Kenny Roota02b8b02010-08-05 16:14:17 -07003267 mRetries++;
3268 if (mRetries > MAX_RETRIES) {
3269 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07003270 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07003271 handleError();
Kenny Roota02b8b02010-08-05 16:14:17 -07003272 } else {
3273 handleExecute();
3274 if (DEBUG_OBB)
3275 Slog.i(TAG, "Posting install MCS_UNBIND");
3276 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
3277 }
3278 } catch (RemoteException e) {
3279 if (DEBUG_OBB)
3280 Slog.i(TAG, "Posting install MCS_RECONNECT");
3281 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
3282 } catch (Exception e) {
3283 if (DEBUG_OBB)
3284 Slog.d(TAG, "Error handling OBB action", e);
3285 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07003286 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07003287 }
3288 }
3289
Kenny Root05105f72010-09-22 17:29:43 -07003290 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07003291 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07003292
3293 protected ObbInfo getObbInfo() throws IOException {
3294 ObbInfo obbInfo;
3295 try {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07003296 obbInfo = mContainerService.getObbInfo(mObbState.canonicalPath);
Kenny Root38cf8862010-09-26 14:18:51 -07003297 } catch (RemoteException e) {
3298 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07003299 + mObbState.canonicalPath);
Kenny Root38cf8862010-09-26 14:18:51 -07003300 obbInfo = null;
3301 }
3302 if (obbInfo == null) {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07003303 throw new IOException("Couldn't read OBB file: " + mObbState.canonicalPath);
Kenny Root38cf8862010-09-26 14:18:51 -07003304 }
3305 return obbInfo;
3306 }
3307
Kenny Rootaf9d6672010-10-08 09:21:39 -07003308 protected void sendNewStatusOrIgnore(int status) {
3309 if (mObbState == null || mObbState.token == null) {
3310 return;
3311 }
3312
Kenny Root38cf8862010-09-26 14:18:51 -07003313 try {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003314 mObbState.token.onObbResult(mObbState.rawPath, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07003315 } catch (RemoteException e) {
3316 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
3317 }
3318 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003319 }
3320
3321 class MountObbAction extends ObbAction {
Ben Komalo444eca22011-09-01 15:17:44 -07003322 private final String mKey;
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003323 private final int mCallingUid;
Kenny Roota02b8b02010-08-05 16:14:17 -07003324
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003325 MountObbAction(ObbState obbState, String key, int callingUid) {
Kenny Roota02b8b02010-08-05 16:14:17 -07003326 super(obbState);
3327 mKey = key;
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003328 mCallingUid = callingUid;
Kenny Roota02b8b02010-08-05 16:14:17 -07003329 }
3330
Jason parks5af0b912010-11-29 09:05:25 -06003331 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07003332 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003333 waitForReady();
3334 warnOnNotMounted();
3335
Kenny Root38cf8862010-09-26 14:18:51 -07003336 final ObbInfo obbInfo = getObbInfo();
3337
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003338 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mCallingUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003339 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
3340 + " which is owned by " + obbInfo.packageName);
3341 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
3342 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07003343 }
3344
Kenny Rootaf9d6672010-10-08 09:21:39 -07003345 final boolean isMounted;
3346 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003347 isMounted = mObbPathToStateMap.containsKey(mObbState.rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003348 }
3349 if (isMounted) {
3350 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
3351 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
3352 return;
3353 }
3354
Kenny Rootaf9d6672010-10-08 09:21:39 -07003355 final String hashedKey;
3356 if (mKey == null) {
3357 hashedKey = "none";
3358 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003359 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07003360 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
3361
3362 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
3363 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
3364 SecretKey key = factory.generateSecret(ks);
3365 BigInteger bi = new BigInteger(key.getEncoded());
3366 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003367 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07003368 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
3369 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
3370 return;
3371 } catch (InvalidKeySpecException e) {
3372 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
3373 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07003374 return;
3375 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07003376 }
Kenny Root38cf8862010-09-26 14:18:51 -07003377
Kenny Rootaf9d6672010-10-08 09:21:39 -07003378 int rc = StorageResultCode.OperationSucceeded;
Kenny Rootaf9d6672010-10-08 09:21:39 -07003379 try {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07003380 mConnector.execute("obb", "mount", mObbState.canonicalPath, new SensitiveArg(hashedKey),
Jeff Sharkey56cd6462013-06-07 15:09:15 -07003381 mObbState.ownerGid);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003382 } catch (NativeDaemonConnectorException e) {
3383 int code = e.getCode();
3384 if (code != VoldResponseCode.OpFailedStorageBusy) {
3385 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07003386 }
3387 }
3388
Kenny Rootaf9d6672010-10-08 09:21:39 -07003389 if (rc == StorageResultCode.OperationSucceeded) {
3390 if (DEBUG_OBB)
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07003391 Slog.d(TAG, "Successfully mounted OBB " + mObbState.canonicalPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003392
3393 synchronized (mObbMounts) {
3394 addObbStateLocked(mObbState);
3395 }
3396
3397 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07003398 } else {
Kenny Root05105f72010-09-22 17:29:43 -07003399 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07003400
Kenny Rootaf9d6672010-10-08 09:21:39 -07003401 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07003402 }
3403 }
3404
Jason parks5af0b912010-11-29 09:05:25 -06003405 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07003406 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003407 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07003408 }
Kenny Roota02b8b02010-08-05 16:14:17 -07003409
3410 @Override
3411 public String toString() {
3412 StringBuilder sb = new StringBuilder();
3413 sb.append("MountObbAction{");
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003414 sb.append(mObbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07003415 sb.append('}');
3416 return sb.toString();
3417 }
3418 }
3419
3420 class UnmountObbAction extends ObbAction {
Ben Komalo444eca22011-09-01 15:17:44 -07003421 private final boolean mForceUnmount;
Kenny Roota02b8b02010-08-05 16:14:17 -07003422
3423 UnmountObbAction(ObbState obbState, boolean force) {
3424 super(obbState);
3425 mForceUnmount = force;
3426 }
3427
Jason parks5af0b912010-11-29 09:05:25 -06003428 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07003429 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003430 waitForReady();
3431 warnOnNotMounted();
3432
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003433 final ObbState existingState;
Kenny Root38cf8862010-09-26 14:18:51 -07003434 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003435 existingState = mObbPathToStateMap.get(mObbState.rawPath);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003436 }
Kenny Root38cf8862010-09-26 14:18:51 -07003437
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003438 if (existingState == null) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003439 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
3440 return;
3441 }
3442
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003443 if (existingState.ownerGid != mObbState.ownerGid) {
3444 Slog.w(TAG, "Permission denied attempting to unmount OBB " + existingState.rawPath
3445 + " (owned by GID " + existingState.ownerGid + ")");
Kenny Rootaf9d6672010-10-08 09:21:39 -07003446 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
3447 return;
3448 }
3449
Kenny Rootaf9d6672010-10-08 09:21:39 -07003450 int rc = StorageResultCode.OperationSucceeded;
Kenny Rootaf9d6672010-10-08 09:21:39 -07003451 try {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07003452 final Command cmd = new Command("obb", "unmount", mObbState.canonicalPath);
Jeff Sharkeydd519fa2011-12-02 14:11:21 -08003453 if (mForceUnmount) {
3454 cmd.appendArg("force");
3455 }
3456 mConnector.execute(cmd);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003457 } catch (NativeDaemonConnectorException e) {
3458 int code = e.getCode();
3459 if (code == VoldResponseCode.OpFailedStorageBusy) {
3460 rc = StorageResultCode.OperationFailedStorageBusy;
3461 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
3462 // If it's not mounted then we've already won.
3463 rc = StorageResultCode.OperationSucceeded;
3464 } else {
3465 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07003466 }
3467 }
3468
Kenny Rootaf9d6672010-10-08 09:21:39 -07003469 if (rc == StorageResultCode.OperationSucceeded) {
3470 synchronized (mObbMounts) {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003471 removeObbStateLocked(existingState);
Kenny Root38cf8862010-09-26 14:18:51 -07003472 }
3473
Kenny Rootaf9d6672010-10-08 09:21:39 -07003474 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07003475 } else {
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003476 Slog.w(TAG, "Could not unmount OBB: " + existingState);
Kenny Rootaf9d6672010-10-08 09:21:39 -07003477 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07003478 }
3479 }
3480
Jason parks5af0b912010-11-29 09:05:25 -06003481 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07003482 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07003483 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07003484 }
3485
3486 @Override
3487 public String toString() {
3488 StringBuilder sb = new StringBuilder();
3489 sb.append("UnmountObbAction{");
Jeff Sharkey4fbbda42012-09-24 18:34:07 -07003490 sb.append(mObbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07003491 sb.append(",force=");
3492 sb.append(mForceUnmount);
Kenny Roota02b8b02010-08-05 16:14:17 -07003493 sb.append('}');
3494 return sb.toString();
3495 }
Kenny Root02c87302010-07-01 08:10:18 -07003496 }
Kenny Root38cf8862010-09-26 14:18:51 -07003497
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07003498 private static class Callbacks extends Handler {
3499 private static final int MSG_STORAGE_STATE_CHANGED = 1;
3500 private static final int MSG_VOLUME_STATE_CHANGED = 2;
Jeff Sharkey50a05452015-04-29 11:24:52 -07003501 private static final int MSG_VOLUME_RECORD_CHANGED = 3;
3502 private static final int MSG_VOLUME_FORGOTTEN = 4;
3503 private static final int MSG_DISK_SCANNED = 5;
Makoto Onuki9dc575d2015-06-12 16:10:25 -07003504 private static final int MSG_DISK_DESTROYED = 6;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07003505
3506 private final RemoteCallbackList<IMountServiceListener>
3507 mCallbacks = new RemoteCallbackList<>();
3508
3509 public Callbacks(Looper looper) {
3510 super(looper);
3511 }
3512
3513 public void register(IMountServiceListener callback) {
3514 mCallbacks.register(callback);
3515 }
3516
3517 public void unregister(IMountServiceListener callback) {
3518 mCallbacks.unregister(callback);
3519 }
3520
3521 @Override
3522 public void handleMessage(Message msg) {
3523 final SomeArgs args = (SomeArgs) msg.obj;
3524 final int n = mCallbacks.beginBroadcast();
3525 for (int i = 0; i < n; i++) {
3526 final IMountServiceListener callback = mCallbacks.getBroadcastItem(i);
3527 try {
3528 invokeCallback(callback, msg.what, args);
3529 } catch (RemoteException ignored) {
3530 }
3531 }
3532 mCallbacks.finishBroadcast();
3533 args.recycle();
3534 }
3535
3536 private void invokeCallback(IMountServiceListener callback, int what, SomeArgs args)
3537 throws RemoteException {
3538 switch (what) {
3539 case MSG_STORAGE_STATE_CHANGED: {
3540 callback.onStorageStateChanged((String) args.arg1, (String) args.arg2,
3541 (String) args.arg3);
3542 break;
3543 }
3544 case MSG_VOLUME_STATE_CHANGED: {
3545 callback.onVolumeStateChanged((VolumeInfo) args.arg1, args.argi2, args.argi3);
3546 break;
3547 }
Jeff Sharkey50a05452015-04-29 11:24:52 -07003548 case MSG_VOLUME_RECORD_CHANGED: {
3549 callback.onVolumeRecordChanged((VolumeRecord) args.arg1);
3550 break;
3551 }
3552 case MSG_VOLUME_FORGOTTEN: {
3553 callback.onVolumeForgotten((String) args.arg1);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07003554 break;
3555 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07003556 case MSG_DISK_SCANNED: {
3557 callback.onDiskScanned((DiskInfo) args.arg1, args.argi2);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07003558 break;
3559 }
Makoto Onuki9dc575d2015-06-12 16:10:25 -07003560 case MSG_DISK_DESTROYED: {
3561 callback.onDiskDestroyed((DiskInfo) args.arg1);
3562 break;
3563 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07003564 }
3565 }
3566
3567 private void notifyStorageStateChanged(String path, String oldState, String newState) {
3568 final SomeArgs args = SomeArgs.obtain();
3569 args.arg1 = path;
3570 args.arg2 = oldState;
3571 args.arg3 = newState;
3572 obtainMessage(MSG_STORAGE_STATE_CHANGED, args).sendToTarget();
3573 }
3574
3575 private void notifyVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
3576 final SomeArgs args = SomeArgs.obtain();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07003577 args.arg1 = vol.clone();
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07003578 args.argi2 = oldState;
3579 args.argi3 = newState;
3580 obtainMessage(MSG_VOLUME_STATE_CHANGED, args).sendToTarget();
3581 }
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07003582
Jeff Sharkey50a05452015-04-29 11:24:52 -07003583 private void notifyVolumeRecordChanged(VolumeRecord rec) {
3584 final SomeArgs args = SomeArgs.obtain();
3585 args.arg1 = rec.clone();
3586 obtainMessage(MSG_VOLUME_RECORD_CHANGED, args).sendToTarget();
3587 }
3588
3589 private void notifyVolumeForgotten(String fsUuid) {
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07003590 final SomeArgs args = SomeArgs.obtain();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07003591 args.arg1 = fsUuid;
Jeff Sharkey50a05452015-04-29 11:24:52 -07003592 obtainMessage(MSG_VOLUME_FORGOTTEN, args).sendToTarget();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -07003593 }
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07003594
Jeff Sharkey620b32b2015-04-23 19:36:02 -07003595 private void notifyDiskScanned(DiskInfo disk, int volumeCount) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07003596 final SomeArgs args = SomeArgs.obtain();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07003597 args.arg1 = disk.clone();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07003598 args.argi2 = volumeCount;
3599 obtainMessage(MSG_DISK_SCANNED, args).sendToTarget();
Jeff Sharkey7e92ef32015-04-17 17:35:07 -07003600 }
Makoto Onuki9dc575d2015-06-12 16:10:25 -07003601
3602 private void notifyDiskDestroyed(DiskInfo disk) {
3603 final SomeArgs args = SomeArgs.obtain();
3604 args.arg1 = disk.clone();
3605 obtainMessage(MSG_DISK_DESTROYED, args).sendToTarget();
3606 }
Jeff Sharkey7151a9a2015-04-04 15:22:37 -07003607 }
3608
Kenny Root38cf8862010-09-26 14:18:51 -07003609 @Override
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003610 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
3611 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
3612
3613 final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ", 160);
Jeff Sharkey27de30d2015-04-18 16:20:27 -07003614 synchronized (mLock) {
3615 pw.println("Disks:");
3616 pw.increaseIndent();
3617 for (int i = 0; i < mDisks.size(); i++) {
3618 final DiskInfo disk = mDisks.valueAt(i);
3619 disk.dump(pw);
3620 }
3621 pw.decreaseIndent();
3622
3623 pw.println();
3624 pw.println("Volumes:");
3625 pw.increaseIndent();
3626 for (int i = 0; i < mVolumes.size(); i++) {
3627 final VolumeInfo vol = mVolumes.valueAt(i);
3628 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) continue;
3629 vol.dump(pw);
3630 }
3631 pw.decreaseIndent();
3632
3633 pw.println();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07003634 pw.println("Records:");
Jeff Sharkey27de30d2015-04-18 16:20:27 -07003635 pw.increaseIndent();
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07003636 for (int i = 0; i < mRecords.size(); i++) {
3637 final VolumeRecord note = mRecords.valueAt(i);
3638 note.dump(pw);
Jeff Sharkey27de30d2015-04-18 16:20:27 -07003639 }
3640 pw.decreaseIndent();
Jeff Sharkey275e3e42015-04-24 16:10:32 -07003641
3642 pw.println();
3643 pw.println("Primary storage UUID: " + mPrimaryStorageUuid);
Jeff Sharkey4c099d02015-05-15 13:45:00 -07003644 pw.println("Force adoptable: " + mForceAdoptable);
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08003645 pw.println();
Jeff Sharkeyce14cd02015-12-07 15:35:42 -07003646 pw.println("Local unlocked users: " + Arrays.toString(mLocalUnlockedUsers));
3647 pw.println("System unlocked users: " + Arrays.toString(mSystemUnlockedUsers));
Jeff Sharkey27de30d2015-04-18 16:20:27 -07003648 }
Kenny Root38cf8862010-09-26 14:18:51 -07003649
Kenny Root38cf8862010-09-26 14:18:51 -07003650 synchronized (mObbMounts) {
Jeff Sharkey27de30d2015-04-18 16:20:27 -07003651 pw.println();
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003652 pw.println("mObbMounts:");
3653 pw.increaseIndent();
3654 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet()
3655 .iterator();
Kenny Rootaf9d6672010-10-08 09:21:39 -07003656 while (binders.hasNext()) {
3657 Entry<IBinder, List<ObbState>> e = binders.next();
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003658 pw.println(e.getKey() + ":");
3659 pw.increaseIndent();
Kenny Rootaf9d6672010-10-08 09:21:39 -07003660 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07003661 for (final ObbState obbState : obbStates) {
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003662 pw.println(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07003663 }
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003664 pw.decreaseIndent();
Kenny Root38cf8862010-09-26 14:18:51 -07003665 }
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003666 pw.decreaseIndent();
Kenny Rootaf9d6672010-10-08 09:21:39 -07003667
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003668 pw.println();
3669 pw.println("mObbPathToStateMap:");
3670 pw.increaseIndent();
Kenny Rootaf9d6672010-10-08 09:21:39 -07003671 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
3672 while (maps.hasNext()) {
3673 final Entry<String, ObbState> e = maps.next();
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003674 pw.print(e.getKey());
3675 pw.print(" -> ");
3676 pw.println(e.getValue());
Kenny Rootaf9d6672010-10-08 09:21:39 -07003677 }
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003678 pw.decreaseIndent();
Kenny Root38cf8862010-09-26 14:18:51 -07003679 }
Kenny Root4161f9b2011-07-13 09:48:33 -07003680
Robert Greenwalt470fd722012-01-18 12:51:15 -08003681 pw.println();
Jeff Sharkey5b0e5202015-12-18 17:18:09 -07003682 pw.println("mConnector:");
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003683 pw.increaseIndent();
Robert Greenwalt470fd722012-01-18 12:51:15 -08003684 mConnector.dump(fd, pw, args);
Jeff Sharkey5aca2b82013-10-16 16:21:54 -07003685 pw.decreaseIndent();
Christopher Tate7265abe2014-11-21 13:54:45 -08003686
Christopher Tate7265abe2014-11-21 13:54:45 -08003687 pw.println();
Jeff Sharkey5b0e5202015-12-18 17:18:09 -07003688 pw.println("mCryptConnector:");
3689 pw.increaseIndent();
3690 mCryptConnector.dump(fd, pw, args);
3691 pw.decreaseIndent();
3692
3693 pw.println();
Christopher Tate7265abe2014-11-21 13:54:45 -08003694 pw.print("Last maintenance: ");
Jeff Sharkeye8a4b662015-06-27 15:43:45 -07003695 pw.println(TimeUtils.formatForLogging(mLastMaintenance));
Kenny Root38cf8862010-09-26 14:18:51 -07003696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07003698 /** {@inheritDoc} */
Jeff Sharkey48877892015-03-18 11:27:19 -07003699 @Override
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07003700 public void monitor() {
3701 if (mConnector != null) {
3702 mConnector.monitor();
3703 }
Paul Lawrence1c62cbb2015-06-03 14:14:52 -07003704 if (mCryptConnector != null) {
3705 mCryptConnector.monitor();
3706 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07003707 }
Svet Ganov6ee871e2015-07-10 14:29:33 -07003708
3709 private final class MountServiceInternalImpl extends MountServiceInternal {
3710 // Not guarded by a lock.
3711 private final CopyOnWriteArrayList<ExternalStorageMountPolicy> mPolicies =
3712 new CopyOnWriteArrayList<>();
3713
3714 @Override
3715 public void addExternalStoragePolicy(ExternalStorageMountPolicy policy) {
3716 // No locking - CopyOnWriteArrayList
3717 mPolicies.add(policy);
3718 }
3719
3720 @Override
3721 public void onExternalStoragePolicyChanged(int uid, String packageName) {
3722 final int mountMode = getExternalStorageMountMode(uid, packageName);
3723 remountUidExternalStorage(uid, mountMode);
3724 }
3725
3726 @Override
3727 public int getExternalStorageMountMode(int uid, String packageName) {
3728 // No locking - CopyOnWriteArrayList
3729 int mountMode = Integer.MAX_VALUE;
3730 for (ExternalStorageMountPolicy policy : mPolicies) {
3731 final int policyMode = policy.getMountMode(uid, packageName);
3732 if (policyMode == Zygote.MOUNT_EXTERNAL_NONE) {
3733 return Zygote.MOUNT_EXTERNAL_NONE;
3734 }
3735 mountMode = Math.min(mountMode, policyMode);
3736 }
3737 if (mountMode == Integer.MAX_VALUE) {
3738 return Zygote.MOUNT_EXTERNAL_NONE;
3739 }
3740 return mountMode;
3741 }
3742
3743 public boolean hasExternalStorage(int uid, String packageName) {
Amith Yamasani2bd5cff2015-07-22 14:42:31 -07003744 // No need to check for system uid. This avoids a deadlock between
3745 // PackageManagerService and AppOpsService.
3746 if (uid == Process.SYSTEM_UID) {
3747 return true;
3748 }
Svet Ganov6ee871e2015-07-10 14:29:33 -07003749 // No locking - CopyOnWriteArrayList
3750 for (ExternalStorageMountPolicy policy : mPolicies) {
3751 final boolean policyHasStorage = policy.hasExternalStorage(uid, packageName);
3752 if (!policyHasStorage) {
3753 return false;
3754 }
3755 }
3756 return true;
3757 }
3758 }
Jeff Sharkeyfa23c5a2011-08-09 21:44:24 -07003759}