The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 19 | import static com.android.internal.util.XmlUtils.readBooleanAttribute; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 20 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 21 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 22 | import static com.android.internal.util.XmlUtils.readStringAttribute; |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 23 | import static com.android.internal.util.XmlUtils.writeBooleanAttribute; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 24 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 25 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 26 | import static com.android.internal.util.XmlUtils.writeStringAttribute; |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 27 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 28 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 29 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
| 30 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 31 | import android.Manifest; |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 32 | import android.annotation.Nullable; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 33 | import android.app.ActivityManager; |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 34 | import android.app.ActivityManagerNative; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 35 | import android.app.AppOpsManager; |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 36 | import android.app.IActivityManager; |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 37 | import android.content.BroadcastReceiver; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 38 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.content.Context; |
| 40 | import android.content.Intent; |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 41 | import android.content.IntentFilter; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 42 | import android.content.ServiceConnection; |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 43 | import android.content.pm.IPackageMoveObserver; |
| 44 | import android.content.pm.PackageManager; |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 45 | import android.content.pm.ProviderInfo; |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 46 | import android.content.pm.UserInfo; |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 47 | import android.content.res.Configuration; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 48 | import android.content.res.ObbInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.net.Uri; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 50 | import android.os.Binder; |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 51 | import android.os.DropBoxManager; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 52 | import android.os.Environment; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 53 | import android.os.Environment.UserEnvironment; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 54 | import android.os.FileUtils; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 55 | import android.os.Handler; |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 56 | import android.os.HandlerThread; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 57 | import android.os.IBinder; |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 58 | import android.os.Looper; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 59 | import android.os.Message; |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 60 | import android.os.ParcelFileDescriptor; |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 61 | import android.os.PowerManager; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 62 | import android.os.Process; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 63 | import android.os.RemoteCallbackList; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 64 | import android.os.RemoteException; |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 65 | import android.os.ServiceManager; |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 66 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 68 | import android.os.UserHandle; |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 69 | import android.os.UserManager; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 70 | import android.os.storage.DiskInfo; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 71 | import android.os.storage.IMountService; |
| 72 | import android.os.storage.IMountServiceListener; |
| 73 | import android.os.storage.IMountShutdownObserver; |
| 74 | import android.os.storage.IObbActionListener; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 75 | import android.os.storage.MountServiceInternal; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 76 | import android.os.storage.OnObbStateChangeListener; |
Paul Lawrence | 46791e7 | 2014-04-03 09:10:26 -0700 | [diff] [blame] | 77 | import android.os.storage.StorageManager; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 78 | import android.os.storage.StorageResultCode; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 79 | import android.os.storage.StorageVolume; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 80 | import android.os.storage.VolumeInfo; |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 81 | import android.os.storage.VolumeRecord; |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 82 | import android.provider.MediaStore; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 83 | import android.provider.Settings; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 84 | import android.text.TextUtils; |
Jeff Sharkey | 1783f14 | 2015-04-17 10:52:51 -0700 | [diff] [blame] | 85 | import android.text.format.DateUtils; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 86 | import android.util.ArrayMap; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 87 | import android.util.AtomicFile; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 88 | import android.util.Log; |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 89 | import android.util.Slog; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 90 | import android.util.TimeUtils; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 91 | import android.util.Xml; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 92 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 93 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 94 | import com.android.internal.app.IMediaContainerService; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 95 | import com.android.internal.os.SomeArgs; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 96 | import com.android.internal.os.Zygote; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 97 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 98 | import com.android.internal.util.FastXmlSerializer; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 99 | import com.android.internal.util.HexDump; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 100 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 101 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 102 | import com.android.internal.widget.LockPatternUtils; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 103 | import com.android.server.NativeDaemonConnector.Command; |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 104 | import com.android.server.NativeDaemonConnector.SensitiveArg; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 105 | import com.android.server.pm.PackageManagerService; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 106 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 107 | import libcore.io.IoUtils; |
| 108 | import libcore.util.EmptyArray; |
| 109 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 110 | import org.xmlpull.v1.XmlPullParser; |
| 111 | import org.xmlpull.v1.XmlPullParserException; |
| 112 | import org.xmlpull.v1.XmlSerializer; |
| 113 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 114 | import java.io.File; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 115 | import java.io.FileDescriptor; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 116 | import java.io.FileInputStream; |
| 117 | import java.io.FileNotFoundException; |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 118 | import java.io.FileOutputStream; |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 119 | import java.io.IOException; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 120 | import java.io.PrintWriter; |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 121 | import java.math.BigInteger; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 122 | import java.nio.charset.StandardCharsets; |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 123 | import java.security.NoSuchAlgorithmException; |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 124 | import java.security.spec.InvalidKeySpecException; |
| 125 | import java.security.spec.KeySpec; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 126 | import java.util.ArrayList; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 127 | import java.util.Arrays; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 128 | import java.util.HashMap; |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 129 | import java.util.HashSet; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 130 | import java.util.Iterator; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 131 | import java.util.LinkedList; |
| 132 | import java.util.List; |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 133 | import java.util.Locale; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 134 | import java.util.Map; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 135 | import java.util.Map.Entry; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 136 | import java.util.Objects; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 137 | import java.util.concurrent.CopyOnWriteArrayList; |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 138 | import java.util.concurrent.CountDownLatch; |
| 139 | import java.util.concurrent.TimeUnit; |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 140 | import java.util.concurrent.TimeoutException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 142 | import javax.crypto.SecretKey; |
| 143 | import javax.crypto.SecretKeyFactory; |
| 144 | import javax.crypto.spec.PBEKeySpec; |
| 145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | /** |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 147 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | */ |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 151 | class MountService extends IMountService.Stub |
| 152 | implements INativeDaemonConnectorCallbacks, Watchdog.Monitor { |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 153 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 154 | // Static direct instance pointer for the tightly-coupled idle service to use |
| 155 | static MountService sSelf = null; |
| 156 | |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 157 | 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); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void onBootPhase(int phase) { |
| 172 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 173 | mMountService.systemReady(); |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 174 | } else if (phase == SystemService.PHASE_BOOT_COMPLETED) { |
| 175 | mMountService.bootCompleted(); |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 178 | |
| 179 | @Override |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 180 | public void onUnlockUser(int userHandle) { |
| 181 | mMountService.onUnlockUser(userHandle); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void onCleanupUser(int userHandle) { |
| 186 | mMountService.onCleanupUser(userHandle); |
| 187 | } |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 190 | private static final boolean DEBUG_EVENTS = false; |
Kenny Root | b7db272 | 2011-01-25 16:39:35 -0800 | [diff] [blame] | 191 | private static final boolean DEBUG_OBB = false; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 192 | |
Kenny Root | 07714d4 | 2011-08-17 17:49:28 -0700 | [diff] [blame] | 193 | // Disable this since it messes up long-running cryptfs operations. |
| 194 | private static final boolean WATCHDOG_ENABLE = false; |
| 195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | private static final String TAG = "MountService"; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 197 | |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 198 | private static final String TAG_STORAGE_BENCHMARK = "storage_benchmark"; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 199 | private static final String TAG_STORAGE_TRIM = "storage_trim"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 201 | private static final String VOLD_TAG = "VoldConnector"; |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 202 | private static final String CRYPTD_TAG = "CryptdConnector"; |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 203 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 204 | /** Maximum number of ASEC containers allowed to be mounted. */ |
| 205 | private static final int MAX_CONTAINERS = 250; |
| 206 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 207 | /** Magic value sent by MoveTask.cpp */ |
| 208 | private static final int MOVE_STATUS_COPY_FINISHED = 82; |
| 209 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 210 | /* |
| 211 | * Internal vold response code constants |
| 212 | */ |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 213 | class VoldResponseCode { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 214 | /* |
| 215 | * 100 series - Requestion action was initiated; expect another reply |
| 216 | * before proceeding with a new command. |
| 217 | */ |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 218 | public static final int VolumeListResult = 110; |
| 219 | public static final int AsecListResult = 111; |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 220 | public static final int StorageUsersListResult = 112; |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 221 | public static final int CryptfsGetfieldResult = 113; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 222 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 223 | /* |
| 224 | * 200 series - Requestion action has been successfully completed. |
| 225 | */ |
| 226 | public static final int ShareStatusResult = 210; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 227 | public static final int AsecPathResult = 211; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 228 | public static final int ShareEnabledResult = 212; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 229 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 230 | /* |
| 231 | * 400 series - Command was accepted, but the requested action |
| 232 | * did not take place. |
| 233 | */ |
| 234 | public static final int OpFailedNoMedia = 401; |
| 235 | public static final int OpFailedMediaBlank = 402; |
| 236 | public static final int OpFailedMediaCorrupt = 403; |
| 237 | public static final int OpFailedVolNotMounted = 404; |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 238 | public static final int OpFailedStorageBusy = 405; |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 239 | public static final int OpFailedStorageNotFound = 406; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * 600 series - Unsolicited broadcasts. |
| 243 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 244 | public static final int DISK_CREATED = 640; |
| 245 | public static final int DISK_SIZE_CHANGED = 641; |
| 246 | public static final int DISK_LABEL_CHANGED = 642; |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 247 | public static final int DISK_SCANNED = 643; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 248 | public static final int DISK_SYS_PATH_CHANGED = 644; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 249 | public static final int DISK_DESTROYED = 649; |
| 250 | |
| 251 | public static final int VOLUME_CREATED = 650; |
| 252 | public static final int VOLUME_STATE_CHANGED = 651; |
| 253 | public static final int VOLUME_FS_TYPE_CHANGED = 652; |
| 254 | public static final int VOLUME_FS_UUID_CHANGED = 653; |
| 255 | public static final int VOLUME_FS_LABEL_CHANGED = 654; |
| 256 | public static final int VOLUME_PATH_CHANGED = 655; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 257 | public static final int VOLUME_INTERNAL_PATH_CHANGED = 656; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 258 | public static final int VOLUME_DESTROYED = 659; |
Svetoslav | f23b64d | 2013-04-25 14:45:54 -0700 | [diff] [blame] | 259 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 260 | public static final int MOVE_STATUS = 660; |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 261 | public static final int BENCHMARK_RESULT = 661; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 262 | public static final int TRIM_RESULT = 662; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 265 | private static final int VERSION_INIT = 1; |
| 266 | private static final int VERSION_ADD_PRIMARY = 2; |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 267 | private static final int VERSION_FIX_PRIMARY = 3; |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 268 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 269 | private static final String TAG_VOLUMES = "volumes"; |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 270 | private static final String ATTR_VERSION = "version"; |
| 271 | private static final String ATTR_PRIMARY_STORAGE_UUID = "primaryStorageUuid"; |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 272 | private static final String ATTR_FORCE_ADOPTABLE = "forceAdoptable"; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 273 | private static final String TAG_VOLUME = "volume"; |
| 274 | private static final String ATTR_TYPE = "type"; |
| 275 | private static final String ATTR_FS_UUID = "fsUuid"; |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 276 | private static final String ATTR_PART_GUID = "partGuid"; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 277 | private static final String ATTR_NICKNAME = "nickname"; |
| 278 | private static final String ATTR_USER_FLAGS = "userFlags"; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 279 | private static final String ATTR_CREATED_MILLIS = "createdMillis"; |
| 280 | private static final String ATTR_LAST_TRIM_MILLIS = "lastTrimMillis"; |
| 281 | private static final String ATTR_LAST_BENCH_MILLIS = "lastBenchMillis"; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 282 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 283 | private final AtomicFile mSettingsFile; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 284 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 285 | /** |
| 286 | * <em>Never</em> hold the lock while performing downcalls into vold, since |
| 287 | * unsolicited events can suddenly appear to update data structures. |
| 288 | */ |
| 289 | private final Object mLock = new Object(); |
| 290 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 291 | /** Set of users that we know are unlocked. */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 292 | @GuardedBy("mLock") |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 293 | private int[] mLocalUnlockedUsers = EmptyArray.INT; |
| 294 | /** Set of users that system knows are unlocked. */ |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 295 | @GuardedBy("mLock") |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 296 | private int[] mSystemUnlockedUsers = EmptyArray.INT; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 297 | |
| 298 | /** Map from disk ID to disk */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 299 | @GuardedBy("mLock") |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 300 | private ArrayMap<String, DiskInfo> mDisks = new ArrayMap<>(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 301 | /** Map from volume ID to disk */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 302 | @GuardedBy("mLock") |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 303 | private final ArrayMap<String, VolumeInfo> mVolumes = new ArrayMap<>(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 304 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 305 | /** Map from UUID to record */ |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 306 | @GuardedBy("mLock") |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 307 | private ArrayMap<String, VolumeRecord> mRecords = new ArrayMap<>(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 308 | @GuardedBy("mLock") |
| 309 | private String mPrimaryStorageUuid; |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 310 | @GuardedBy("mLock") |
| 311 | private boolean mForceAdoptable; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 312 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 313 | /** Map from disk ID to latches */ |
| 314 | @GuardedBy("mLock") |
| 315 | private ArrayMap<String, CountDownLatch> mDiskScanLatches = new ArrayMap<>(); |
| 316 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 317 | @GuardedBy("mLock") |
| 318 | private IPackageMoveObserver mMoveCallback; |
| 319 | @GuardedBy("mLock") |
| 320 | private String mMoveTargetUuid; |
| 321 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 322 | private VolumeInfo findVolumeByIdOrThrow(String id) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 323 | synchronized (mLock) { |
| 324 | final VolumeInfo vol = mVolumes.get(id); |
| 325 | if (vol != null) { |
| 326 | return vol; |
| 327 | } |
| 328 | } |
| 329 | throw new IllegalArgumentException("No volume found for ID " + id); |
| 330 | } |
| 331 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 332 | private String findVolumeIdForPathOrThrow(String path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 333 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 334 | for (int i = 0; i < mVolumes.size(); i++) { |
| 335 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 336 | if (vol.path != null && path.startsWith(vol.path)) { |
| 337 | return vol.id; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 341 | throw new IllegalArgumentException("No volume found for path " + path); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 344 | private VolumeRecord findRecordForPath(String path) { |
| 345 | synchronized (mLock) { |
| 346 | for (int i = 0; i < mVolumes.size(); i++) { |
| 347 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 348 | if (vol.path != null && path.startsWith(vol.path)) { |
| 349 | return mRecords.get(vol.fsUuid); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | return null; |
| 354 | } |
| 355 | |
| 356 | private String scrubPath(String path) { |
| 357 | if (path.startsWith(Environment.getDataDirectory().getAbsolutePath())) { |
| 358 | return "internal"; |
| 359 | } |
| 360 | final VolumeRecord rec = findRecordForPath(path); |
| 361 | if (rec == null || rec.createdMillis == 0) { |
| 362 | return "unknown"; |
| 363 | } else { |
| 364 | return "ext:" + (int) ((System.currentTimeMillis() - rec.createdMillis) |
| 365 | / DateUtils.WEEK_IN_MILLIS) + "w"; |
| 366 | } |
| 367 | } |
| 368 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 369 | private @Nullable VolumeInfo findStorageForUuid(String volumeUuid) { |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 370 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
| 371 | if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) { |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 372 | return storage.findVolumeById(VolumeInfo.ID_EMULATED_INTERNAL); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 373 | } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) { |
| 374 | return storage.getPrimaryPhysicalVolume(); |
| 375 | } else { |
| 376 | return storage.findEmulatedForPrivate(storage.findVolumeByUuid(volumeUuid)); |
| 377 | } |
| 378 | } |
| 379 | |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 380 | private boolean shouldBenchmark() { |
| 381 | final long benchInterval = Settings.Global.getLong(mContext.getContentResolver(), |
| 382 | Settings.Global.STORAGE_BENCHMARK_INTERVAL, DateUtils.WEEK_IN_MILLIS); |
Jeff Sharkey | e83d8a9 | 2015-09-09 14:53:38 -0700 | [diff] [blame] | 383 | if (benchInterval == -1) { |
| 384 | return false; |
| 385 | } else if (benchInterval == 0) { |
| 386 | return true; |
| 387 | } |
| 388 | |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 389 | synchronized (mLock) { |
| 390 | for (int i = 0; i < mVolumes.size(); i++) { |
| 391 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 392 | final VolumeRecord rec = mRecords.get(vol.fsUuid); |
Jeff Sharkey | e83d8a9 | 2015-09-09 14:53:38 -0700 | [diff] [blame] | 393 | if (vol.isMountedWritable() && rec != null) { |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 394 | final long benchAge = System.currentTimeMillis() - rec.lastBenchMillis; |
| 395 | if (benchAge >= benchInterval) { |
| 396 | return true; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | return false; |
| 401 | } |
| 402 | } |
| 403 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 404 | private CountDownLatch findOrCreateDiskScanLatch(String diskId) { |
| 405 | synchronized (mLock) { |
| 406 | CountDownLatch latch = mDiskScanLatches.get(diskId); |
| 407 | if (latch == null) { |
| 408 | latch = new CountDownLatch(1); |
| 409 | mDiskScanLatches.put(diskId, latch); |
| 410 | } |
| 411 | return latch; |
| 412 | } |
| 413 | } |
| 414 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 415 | private static String escapeNull(String arg) { |
| 416 | if (TextUtils.isEmpty(arg)) { |
| 417 | return "!"; |
| 418 | } else { |
| 419 | if (arg.indexOf('\0') != -1 || arg.indexOf(' ') != -1) { |
| 420 | throw new IllegalArgumentException(arg); |
| 421 | } |
| 422 | return arg; |
| 423 | } |
| 424 | } |
| 425 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 426 | /** List of crypto types. |
| 427 | * These must match CRYPT_TYPE_XXX in cryptfs.h AND their |
| 428 | * corresponding commands in CommandListener.cpp */ |
| 429 | public static final String[] CRYPTO_TYPES |
| 430 | = { "password", "default", "pattern", "pin" }; |
| 431 | |
Brian Carlstrom | 7395a8a | 2014-04-28 22:11:01 -0700 | [diff] [blame] | 432 | private final Context mContext; |
Brian Carlstrom | dfad99a | 2014-05-07 15:21:14 -0700 | [diff] [blame] | 433 | private final NativeDaemonConnector mConnector; |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 434 | private final NativeDaemonConnector mCryptConnector; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 435 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 436 | private volatile boolean mSystemReady = false; |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 437 | private volatile boolean mBootCompleted = false; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 438 | private volatile boolean mDaemonConnected = false; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 439 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 440 | private PackageManagerService mPms; |
| 441 | |
| 442 | private final Callbacks mCallbacks; |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 443 | private final LockPatternUtils mLockPatternUtils; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 444 | |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 445 | // Two connectors - mConnector & mCryptConnector |
| 446 | private final CountDownLatch mConnectedSignal = new CountDownLatch(2); |
Jeff Sharkey | 0be607c | 2012-11-14 14:39:19 -0800 | [diff] [blame] | 447 | private final CountDownLatch mAsecsScanned = new CountDownLatch(1); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 448 | |
| 449 | private final Object mUnmountLock = new Object(); |
| 450 | @GuardedBy("mUnmountLock") |
| 451 | private CountDownLatch mUnmountSignal; |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 452 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 453 | /** |
| 454 | * Private hash of currently mounted secure containers. |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 455 | * Used as a lock in methods to manipulate secure containers. |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 456 | */ |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 457 | final private HashSet<String> mAsecMountSet = new HashSet<String>(); |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 458 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 459 | /** |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 460 | * The size of the crypto algorithm key in bits for OBB files. Currently |
| 461 | * Twofish is used which takes 128-bit keys. |
| 462 | */ |
| 463 | private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128; |
| 464 | |
| 465 | /** |
| 466 | * The number of times to run SHA1 in the PBKDF2 function for OBB files. |
| 467 | * 1024 is reasonably secure and not too slow. |
| 468 | */ |
| 469 | private static final int PBKDF2_HASH_ROUNDS = 1024; |
| 470 | |
| 471 | /** |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 472 | * Mounted OBB tracking information. Used to track the current state of all |
| 473 | * OBBs. |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 474 | */ |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 475 | final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 476 | |
| 477 | /** Map from raw paths to {@link ObbState}. */ |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 478 | final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>(); |
| 479 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 480 | // Not guarded by a lock. |
| 481 | private final MountServiceInternalImpl mMountServiceInternal = new MountServiceInternalImpl(); |
| 482 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 483 | class ObbState implements IBinder.DeathRecipient { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 484 | public ObbState(String rawPath, String canonicalPath, int callingUid, |
| 485 | IObbActionListener token, int nonce) { |
| 486 | this.rawPath = rawPath; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 487 | this.canonicalPath = canonicalPath; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 488 | |
| 489 | this.ownerGid = UserHandle.getSharedAppGid(callingUid); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 490 | this.token = token; |
| 491 | this.nonce = nonce; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 494 | final String rawPath; |
| 495 | final String canonicalPath; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 496 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 497 | final int ownerGid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 498 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 499 | // Token of remote Binder caller |
| 500 | final IObbActionListener token; |
| 501 | |
| 502 | // Identifier to pass back to the token |
| 503 | final int nonce; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 504 | |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 505 | public IBinder getBinder() { |
| 506 | return token.asBinder(); |
| 507 | } |
| 508 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 509 | @Override |
| 510 | public void binderDied() { |
| 511 | ObbAction action = new UnmountObbAction(this, true); |
| 512 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 513 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 514 | |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 515 | public void link() throws RemoteException { |
| 516 | getBinder().linkToDeath(this, 0); |
| 517 | } |
| 518 | |
| 519 | public void unlink() { |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 520 | getBinder().unlinkToDeath(this, 0); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 521 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 522 | |
| 523 | @Override |
| 524 | public String toString() { |
| 525 | StringBuilder sb = new StringBuilder("ObbState{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 526 | sb.append("rawPath=").append(rawPath); |
| 527 | sb.append(",canonicalPath=").append(canonicalPath); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 528 | sb.append(",ownerGid=").append(ownerGid); |
| 529 | sb.append(",token=").append(token); |
| 530 | sb.append(",binder=").append(getBinder()); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 531 | sb.append('}'); |
| 532 | return sb.toString(); |
| 533 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | // OBB Action Handler |
| 537 | final private ObbActionHandler mObbActionHandler; |
| 538 | |
| 539 | // OBB action handler messages |
| 540 | private static final int OBB_RUN_ACTION = 1; |
| 541 | private static final int OBB_MCS_BOUND = 2; |
| 542 | private static final int OBB_MCS_UNBIND = 3; |
| 543 | private static final int OBB_MCS_RECONNECT = 4; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 544 | private static final int OBB_FLUSH_MOUNT_STATE = 5; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 545 | |
| 546 | /* |
| 547 | * Default Container Service information |
| 548 | */ |
| 549 | static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName( |
| 550 | "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService"); |
| 551 | |
| 552 | final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection(); |
| 553 | |
| 554 | class DefaultContainerConnection implements ServiceConnection { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 555 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 556 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 557 | if (DEBUG_OBB) |
| 558 | Slog.i(TAG, "onServiceConnected"); |
| 559 | IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service); |
| 560 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs)); |
| 561 | } |
| 562 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 563 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 564 | public void onServiceDisconnected(ComponentName name) { |
| 565 | if (DEBUG_OBB) |
| 566 | Slog.i(TAG, "onServiceDisconnected"); |
| 567 | } |
| 568 | }; |
| 569 | |
| 570 | // Used in the ObbActionHandler |
| 571 | private IMediaContainerService mContainerService = null; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 572 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 573 | // Last fstrim operation tracking |
| 574 | private static final String LAST_FSTRIM_FILE = "last-fstrim"; |
| 575 | private final File mLastMaintenanceFile; |
| 576 | private long mLastMaintenance; |
| 577 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 578 | // Handler messages |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 579 | private static final int H_SYSTEM_READY = 1; |
| 580 | private static final int H_DAEMON_CONNECTED = 2; |
| 581 | private static final int H_SHUTDOWN = 3; |
| 582 | private static final int H_FSTRIM = 4; |
| 583 | private static final int H_VOLUME_MOUNT = 5; |
| 584 | private static final int H_VOLUME_BROADCAST = 6; |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 585 | private static final int H_INTERNAL_BROADCAST = 7; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 586 | private static final int H_VOLUME_UNMOUNT = 8; |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 587 | private static final int H_PARTITION_FORGET = 9; |
| 588 | private static final int H_RESET = 10; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 589 | |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 590 | class MountServiceHandler extends Handler { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 591 | public MountServiceHandler(Looper looper) { |
| 592 | super(looper); |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 595 | @Override |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 596 | public void handleMessage(Message msg) { |
| 597 | switch (msg.what) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 598 | case H_SYSTEM_READY: { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 599 | handleSystemReady(); |
| 600 | break; |
| 601 | } |
| 602 | case H_DAEMON_CONNECTED: { |
| 603 | handleDaemonConnected(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 604 | break; |
| 605 | } |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 606 | case H_FSTRIM: { |
Jeff Sharkey | 1783f14 | 2015-04-17 10:52:51 -0700 | [diff] [blame] | 607 | if (!isReady()) { |
| 608 | Slog.i(TAG, "fstrim requested, but no daemon connection yet; trying again"); |
Christopher Tate | 7618db1 | 2015-04-28 16:32:55 -0700 | [diff] [blame] | 609 | sendMessageDelayed(obtainMessage(H_FSTRIM, msg.obj), |
| 610 | DateUtils.SECOND_IN_MILLIS); |
| 611 | break; |
Jeff Sharkey | 1783f14 | 2015-04-17 10:52:51 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 614 | Slog.i(TAG, "Running fstrim idle maintenance"); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 615 | |
| 616 | // Remember when we kicked it off |
| 617 | try { |
| 618 | mLastMaintenance = System.currentTimeMillis(); |
| 619 | mLastMaintenanceFile.setLastModified(mLastMaintenance); |
| 620 | } catch (Exception e) { |
| 621 | Slog.e(TAG, "Unable to record last fstrim!"); |
| 622 | } |
| 623 | |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 624 | final boolean shouldBenchmark = shouldBenchmark(); |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 625 | try { |
| 626 | // This method must be run on the main (handler) thread, |
| 627 | // so it is safe to directly call into vold. |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 628 | mConnector.execute("fstrim", shouldBenchmark ? "dotrimbench" : "dotrim"); |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 629 | } catch (NativeDaemonConnectorException ndce) { |
| 630 | Slog.e(TAG, "Failed to run fstrim!"); |
| 631 | } |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 632 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 633 | // invoke the completion callback, if any |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 634 | // TODO: fstrim is non-blocking, so remove this useless callback |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 635 | Runnable callback = (Runnable) msg.obj; |
| 636 | if (callback != null) { |
| 637 | callback.run(); |
| 638 | } |
| 639 | break; |
| 640 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 641 | case H_SHUTDOWN: { |
| 642 | final IMountShutdownObserver obs = (IMountShutdownObserver) msg.obj; |
| 643 | boolean success = false; |
| 644 | try { |
| 645 | success = mConnector.execute("volume", "shutdown").isClassOk(); |
| 646 | } catch (NativeDaemonConnectorException ignored) { |
| 647 | } |
| 648 | if (obs != null) { |
| 649 | try { |
| 650 | obs.onShutDownComplete(success ? 0 : -1); |
| 651 | } catch (RemoteException ignored) { |
| 652 | } |
| 653 | } |
| 654 | break; |
| 655 | } |
| 656 | case H_VOLUME_MOUNT: { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 657 | final VolumeInfo vol = (VolumeInfo) msg.obj; |
Jeff Sharkey | 2e606d7 | 2015-07-27 14:19:54 -0700 | [diff] [blame] | 658 | if (isMountDisallowed(vol)) { |
| 659 | Slog.i(TAG, "Ignoring mount " + vol.getId() + " due to policy"); |
| 660 | break; |
| 661 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 662 | try { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 663 | mConnector.execute("volume", "mount", vol.id, vol.mountFlags, |
| 664 | vol.mountUserId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 665 | } catch (NativeDaemonConnectorException ignored) { |
| 666 | } |
| 667 | break; |
| 668 | } |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 669 | case H_VOLUME_UNMOUNT: { |
| 670 | final VolumeInfo vol = (VolumeInfo) msg.obj; |
| 671 | unmount(vol.getId()); |
| 672 | break; |
| 673 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 674 | case H_VOLUME_BROADCAST: { |
| 675 | final StorageVolume userVol = (StorageVolume) msg.obj; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 676 | final String envState = userVol.getState(); |
| 677 | Slog.d(TAG, "Volume " + userVol.getId() + " broadcasting " + envState + " to " |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 678 | + userVol.getOwner()); |
| 679 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 680 | final String action = VolumeInfo.getBroadcastForEnvironment(envState); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 681 | if (action != null) { |
| 682 | final Intent intent = new Intent(action, |
| 683 | Uri.fromFile(userVol.getPathFile())); |
| 684 | intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, userVol); |
| 685 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 686 | mContext.sendBroadcastAsUser(intent, userVol.getOwner()); |
| 687 | } |
| 688 | break; |
| 689 | } |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 690 | case H_INTERNAL_BROADCAST: { |
| 691 | // Internal broadcasts aimed at system components, not for |
| 692 | // third-party apps. |
| 693 | final Intent intent = (Intent) msg.obj; |
| 694 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, |
| 695 | android.Manifest.permission.WRITE_MEDIA_STORAGE); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 696 | break; |
| 697 | } |
| 698 | case H_PARTITION_FORGET: { |
| 699 | final String partGuid = (String) msg.obj; |
| 700 | forgetPartition(partGuid); |
| 701 | break; |
| 702 | } |
| 703 | case H_RESET: { |
| 704 | resetIfReadyAndConnected(); |
| 705 | break; |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 706 | } |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 707 | } |
| 708 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 709 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 710 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 711 | private final Handler mHandler; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 712 | |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 713 | private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
| 714 | @Override |
| 715 | public void onReceive(Context context, Intent intent) { |
| 716 | final String action = intent.getAction(); |
| 717 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 718 | Preconditions.checkArgument(userId >= 0); |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 719 | |
| 720 | try { |
| 721 | if (Intent.ACTION_USER_ADDED.equals(action)) { |
| 722 | final UserManager um = mContext.getSystemService(UserManager.class); |
| 723 | final int userSerialNumber = um.getUserSerialNumber(userId); |
| 724 | mConnector.execute("volume", "user_added", userId, userSerialNumber); |
| 725 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 726 | synchronized (mVolumes) { |
| 727 | final int size = mVolumes.size(); |
| 728 | for (int i = 0; i < size; i++) { |
| 729 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 730 | if (vol.mountUserId == userId) { |
| 731 | vol.mountUserId = UserHandle.USER_NULL; |
| 732 | mHandler.obtainMessage(H_VOLUME_UNMOUNT, vol).sendToTarget(); |
| 733 | } |
| 734 | } |
| 735 | } |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 736 | mConnector.execute("volume", "user_removed", userId); |
| 737 | } |
| 738 | } catch (NativeDaemonConnectorException e) { |
| 739 | Slog.w(TAG, "Failed to send user details to vold", e); |
| 740 | } |
| 741 | } |
| 742 | }; |
| 743 | |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 744 | @Override |
| 745 | public void waitForAsecScan() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 746 | waitForLatch(mAsecsScanned, "mAsecsScanned"); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 747 | } |
| 748 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 749 | private void waitForReady() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 750 | waitForLatch(mConnectedSignal, "mConnectedSignal"); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 753 | private void waitForLatch(CountDownLatch latch, String condition) { |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 754 | try { |
| 755 | waitForLatch(latch, condition, -1); |
| 756 | } catch (TimeoutException ignored) { |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | private void waitForLatch(CountDownLatch latch, String condition, long timeoutMillis) |
| 761 | throws TimeoutException { |
| 762 | final long startMillis = SystemClock.elapsedRealtime(); |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 763 | while (true) { |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 764 | try { |
| 765 | if (latch.await(5000, TimeUnit.MILLISECONDS)) { |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 766 | return; |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 767 | } else { |
| 768 | Slog.w(TAG, "Thread " + Thread.currentThread().getName() |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 769 | + " still waiting for " + condition + "..."); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 770 | } |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 771 | } catch (InterruptedException e) { |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 772 | Slog.w(TAG, "Interrupt while waiting for " + condition); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 773 | } |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 774 | if (timeoutMillis > 0 && SystemClock.elapsedRealtime() > startMillis + timeoutMillis) { |
| 775 | throw new TimeoutException("Thread " + Thread.currentThread().getName() |
| 776 | + " gave up waiting for " + condition + " after " + timeoutMillis + "ms"); |
| 777 | } |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 778 | } |
San Mehat | 1f6301e | 2010-01-07 22:40:27 -0800 | [diff] [blame] | 779 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 780 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 781 | private boolean isReady() { |
| 782 | try { |
| 783 | return mConnectedSignal.await(0, TimeUnit.MILLISECONDS); |
| 784 | } catch (InterruptedException e) { |
| 785 | return false; |
| 786 | } |
| 787 | } |
| 788 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 789 | private void handleSystemReady() { |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 790 | initIfReadyAndConnected(); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 791 | resetIfReadyAndConnected(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 792 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 793 | // Start scheduling nominally-daily fstrim operations |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 794 | MountServiceIdler.scheduleIdlePass(mContext); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 797 | /** |
| 798 | * MediaProvider has a ton of code that makes assumptions about storage |
| 799 | * paths never changing, so we outright kill them to pick up new state. |
| 800 | */ |
| 801 | @Deprecated |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 802 | private void killMediaProvider(List<UserInfo> users) { |
| 803 | if (users == null) return; |
| 804 | |
Jeff Sharkey | b3cf953 | 2015-07-17 15:12:39 -0700 | [diff] [blame] | 805 | final long token = Binder.clearCallingIdentity(); |
| 806 | try { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 807 | for (UserInfo user : users) { |
| 808 | // System user does not have media provider, so skip. |
| 809 | if (user.isSystemOnly()) continue; |
| 810 | |
Jeff Sharkey | 2a9e3f8 | 2015-12-18 10:57:58 -0700 | [diff] [blame] | 811 | final ProviderInfo provider = mPms.resolveContentProvider(MediaStore.AUTHORITY, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 812 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 813 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 814 | user.id); |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 815 | if (provider != null) { |
| 816 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 817 | try { |
| 818 | am.killApplicationWithAppId(provider.applicationInfo.packageName, |
| 819 | UserHandle.getAppId(provider.applicationInfo.uid), "vold reset"); |
| 820 | // We only need to run this once. It will kill all users' media processes. |
| 821 | break; |
| 822 | } catch (RemoteException e) { |
| 823 | } |
Jeff Sharkey | b3cf953 | 2015-07-17 15:12:39 -0700 | [diff] [blame] | 824 | } |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 825 | } |
Jeff Sharkey | b3cf953 | 2015-07-17 15:12:39 -0700 | [diff] [blame] | 826 | } finally { |
| 827 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 831 | private void addInternalVolumeLocked() { |
Amith Yamasani | a789248 | 2015-08-07 11:09:05 -0700 | [diff] [blame] | 832 | // Create a stub volume that represents internal storage |
| 833 | final VolumeInfo internal = new VolumeInfo(VolumeInfo.ID_PRIVATE_INTERNAL, |
| 834 | VolumeInfo.TYPE_PRIVATE, null, null); |
| 835 | internal.state = VolumeInfo.STATE_MOUNTED; |
| 836 | internal.path = Environment.getDataDirectory().getAbsolutePath(); |
| 837 | mVolumes.put(internal.id, internal); |
| 838 | } |
| 839 | |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 840 | private void initIfReadyAndConnected() { |
| 841 | Slog.d(TAG, "Thinking about init, mSystemReady=" + mSystemReady |
| 842 | + ", mDaemonConnected=" + mDaemonConnected); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 843 | if (mSystemReady && mDaemonConnected |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 844 | && !StorageManager.isFileEncryptedNativeOnly()) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 845 | // When booting a device without native support, make sure that our |
| 846 | // user directories are locked or unlocked based on the current |
| 847 | // emulation status. |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 848 | final boolean initLocked = StorageManager.isFileEncryptedEmulatedOnly(); |
Paul Crowley | d94ab73 | 2016-02-15 06:44:51 +0000 | [diff] [blame] | 849 | Slog.d(TAG, "Setting up emulation state, initlocked=" + initLocked); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 850 | final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 851 | for (UserInfo user : users) { |
| 852 | try { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 853 | if (initLocked) { |
| 854 | mCryptConnector.execute("cryptfs", "lock_user_key", user.id); |
| 855 | } else { |
| 856 | mCryptConnector.execute("cryptfs", "unlock_user_key", user.id, |
Paul Crowley | d94ab73 | 2016-02-15 06:44:51 +0000 | [diff] [blame] | 857 | user.serialNumber, "!", "!"); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 858 | } |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 859 | } catch (NativeDaemonConnectorException e) { |
| 860 | Slog.w(TAG, "Failed to init vold", e); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 866 | private void resetIfReadyAndConnected() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 867 | Slog.d(TAG, "Thinking about reset, mSystemReady=" + mSystemReady |
| 868 | + ", mDaemonConnected=" + mDaemonConnected); |
| 869 | if (mSystemReady && mDaemonConnected) { |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 870 | final List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 871 | killMediaProvider(users); |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 872 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 873 | final int[] systemUnlockedUsers; |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 874 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 875 | systemUnlockedUsers = mSystemUnlockedUsers; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 876 | |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 877 | mDisks.clear(); |
| 878 | mVolumes.clear(); |
| 879 | |
| 880 | addInternalVolumeLocked(); |
| 881 | } |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 882 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 883 | try { |
| 884 | mConnector.execute("volume", "reset"); |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 885 | |
| 886 | // Tell vold about all existing and started users |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 887 | for (UserInfo user : users) { |
| 888 | mConnector.execute("volume", "user_added", user.id, user.serialNumber); |
| 889 | } |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 890 | for (int userId : systemUnlockedUsers) { |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 891 | mConnector.execute("volume", "user_started", userId); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 892 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 893 | } catch (NativeDaemonConnectorException e) { |
| 894 | Slog.w(TAG, "Failed to reset vold", e); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 895 | } |
| 896 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 897 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 898 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 899 | private void onUnlockUser(int userId) { |
| 900 | Slog.d(TAG, "onUnlockUser " + userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 901 | |
| 902 | // We purposefully block here to make sure that user-specific |
| 903 | // staging area is ready so it's ready for zygote-forked apps to |
| 904 | // bind mount against. |
| 905 | try { |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 906 | mConnector.execute("volume", "user_started", userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 907 | } catch (NativeDaemonConnectorException ignored) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 908 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 909 | |
| 910 | // Record user as started so newly mounted volumes kick off events |
| 911 | // correctly, then synthesize events for any already-mounted volumes. |
| 912 | synchronized (mVolumes) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 913 | for (int i = 0; i < mVolumes.size(); i++) { |
| 914 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 915 | if (vol.isVisibleForRead(userId) && vol.isMountedReadable()) { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 916 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, false); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 917 | mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 918 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 919 | final String envState = VolumeInfo.getEnvironmentForState(vol.getState()); |
| 920 | mCallbacks.notifyStorageStateChanged(userVol.getPath(), envState, envState); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 921 | } |
| 922 | } |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 923 | mSystemUnlockedUsers = ArrayUtils.appendInt(mSystemUnlockedUsers, userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
| 927 | private void onCleanupUser(int userId) { |
| 928 | Slog.d(TAG, "onCleanupUser " + userId); |
| 929 | |
| 930 | try { |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 931 | mConnector.execute("volume", "user_stopped", userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 932 | } catch (NativeDaemonConnectorException ignored) { |
| 933 | } |
| 934 | |
| 935 | synchronized (mVolumes) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 936 | mSystemUnlockedUsers = ArrayUtils.removeInt(mSystemUnlockedUsers, userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 939 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 940 | void runIdleMaintenance(Runnable callback) { |
| 941 | mHandler.sendMessage(mHandler.obtainMessage(H_FSTRIM, callback)); |
| 942 | } |
| 943 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 944 | // Binder entry point for kicking off an immediate fstrim |
| 945 | @Override |
| 946 | public void runMaintenance() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 947 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 948 | runIdleMaintenance(null); |
| 949 | } |
| 950 | |
| 951 | @Override |
| 952 | public long lastMaintenance() { |
| 953 | return mLastMaintenance; |
| 954 | } |
| 955 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 956 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 957 | * Callback from NativeDaemonConnector |
| 958 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 959 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 960 | public void onDaemonConnected() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 961 | mDaemonConnected = true; |
| 962 | mHandler.obtainMessage(H_DAEMON_CONNECTED).sendToTarget(); |
| 963 | } |
| 964 | |
| 965 | private void handleDaemonConnected() { |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 966 | initIfReadyAndConnected(); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 967 | resetIfReadyAndConnected(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 968 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 969 | /* |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 970 | * Now that we've done our initialization, release |
| 971 | * the hounds! |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 972 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 973 | mConnectedSignal.countDown(); |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 974 | if (mConnectedSignal.getCount() != 0) { |
| 975 | // More daemons need to connect |
| 976 | return; |
| 977 | } |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 978 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 979 | // On an encrypted device we can't see system properties yet, so pull |
| 980 | // the system locale out of the mount service. |
| 981 | if ("".equals(SystemProperties.get("vold.encrypt_progress"))) { |
| 982 | copyLocaleFromMountService(); |
| 983 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 984 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 985 | // Let package manager load internal ASECs. |
| 986 | mPms.scanAvailableAsecs(); |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 987 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 988 | // Notify people waiting for ASECs to be scanned that it's done. |
| 989 | mAsecsScanned.countDown(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 992 | private void copyLocaleFromMountService() { |
| 993 | String systemLocale; |
| 994 | try { |
| 995 | systemLocale = getField(StorageManager.SYSTEM_LOCALE_KEY); |
| 996 | } catch (RemoteException e) { |
| 997 | return; |
| 998 | } |
| 999 | if (TextUtils.isEmpty(systemLocale)) { |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | Slog.d(TAG, "Got locale " + systemLocale + " from mount service"); |
| 1004 | Locale locale = Locale.forLanguageTag(systemLocale); |
| 1005 | Configuration config = new Configuration(); |
| 1006 | config.setLocale(locale); |
| 1007 | try { |
| 1008 | ActivityManagerNative.getDefault().updateConfiguration(config); |
| 1009 | } catch (RemoteException e) { |
| 1010 | Slog.e(TAG, "Error setting system locale from mount service", e); |
| 1011 | } |
Elliott Hughes | 9c33f28 | 2014-10-13 12:39:56 -0700 | [diff] [blame] | 1012 | |
| 1013 | // Temporary workaround for http://b/17945169. |
| 1014 | Slog.d(TAG, "Setting system properties to " + systemLocale + " from mount service"); |
Narayan Kamath | d30dbb8 | 2015-01-15 14:48:15 +0000 | [diff] [blame] | 1015 | SystemProperties.set("persist.sys.locale", locale.toLanguageTag()); |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1018 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1019 | * Callback from NativeDaemonConnector |
| 1020 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1021 | @Override |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1022 | public boolean onCheckHoldWakeLock(int code) { |
| 1023 | return false; |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * Callback from NativeDaemonConnector |
| 1028 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1029 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1030 | public boolean onEvent(int code, String raw, String[] cooked) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1031 | synchronized (mLock) { |
| 1032 | return onEventLocked(code, raw, cooked); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1033 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1034 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 1035 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1036 | private boolean onEventLocked(int code, String raw, String[] cooked) { |
| 1037 | switch (code) { |
| 1038 | case VoldResponseCode.DISK_CREATED: { |
| 1039 | if (cooked.length != 3) break; |
| 1040 | final String id = cooked[1]; |
Jeff Sharkey | 74acbbb | 2015-04-21 12:14:03 -0700 | [diff] [blame] | 1041 | int flags = Integer.parseInt(cooked[2]); |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 1042 | if (SystemProperties.getBoolean(StorageManager.PROP_FORCE_ADOPTABLE, false) |
| 1043 | || mForceAdoptable) { |
Jeff Sharkey | 74acbbb | 2015-04-21 12:14:03 -0700 | [diff] [blame] | 1044 | flags |= DiskInfo.FLAG_ADOPTABLE; |
| 1045 | } |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1046 | mDisks.put(id, new DiskInfo(id, flags)); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1047 | break; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 1048 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1049 | case VoldResponseCode.DISK_SIZE_CHANGED: { |
| 1050 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1051 | final DiskInfo disk = mDisks.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1052 | if (disk != null) { |
| 1053 | disk.size = Long.parseLong(cooked[2]); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1054 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1055 | break; |
| 1056 | } |
| 1057 | case VoldResponseCode.DISK_LABEL_CHANGED: { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1058 | final DiskInfo disk = mDisks.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1059 | if (disk != null) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 1060 | final StringBuilder builder = new StringBuilder(); |
| 1061 | for (int i = 2; i < cooked.length; i++) { |
| 1062 | builder.append(cooked[i]).append(' '); |
| 1063 | } |
| 1064 | disk.label = builder.toString().trim(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1065 | } |
| 1066 | break; |
| 1067 | } |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1068 | case VoldResponseCode.DISK_SCANNED: { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 1069 | if (cooked.length != 2) break; |
| 1070 | final DiskInfo disk = mDisks.get(cooked[1]); |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1071 | if (disk != null) { |
| 1072 | onDiskScannedLocked(disk); |
| 1073 | } |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 1074 | break; |
| 1075 | } |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1076 | case VoldResponseCode.DISK_SYS_PATH_CHANGED: { |
| 1077 | if (cooked.length != 3) break; |
| 1078 | final DiskInfo disk = mDisks.get(cooked[1]); |
| 1079 | if (disk != null) { |
| 1080 | disk.sysPath = cooked[2]; |
| 1081 | } |
| 1082 | break; |
| 1083 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1084 | case VoldResponseCode.DISK_DESTROYED: { |
| 1085 | if (cooked.length != 2) break; |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 1086 | final DiskInfo disk = mDisks.remove(cooked[1]); |
| 1087 | if (disk != null) { |
| 1088 | mCallbacks.notifyDiskDestroyed(disk); |
| 1089 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1090 | break; |
| 1091 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1092 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1093 | case VoldResponseCode.VOLUME_CREATED: { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1094 | final String id = cooked[1]; |
| 1095 | final int type = Integer.parseInt(cooked[2]); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1096 | final String diskId = TextUtils.nullIfEmpty(cooked[3]); |
| 1097 | final String partGuid = TextUtils.nullIfEmpty(cooked[4]); |
| 1098 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 1099 | final DiskInfo disk = mDisks.get(diskId); |
Jeff Sharkey | 5af1835d | 2015-07-07 17:26:59 -0700 | [diff] [blame] | 1100 | final VolumeInfo vol = new VolumeInfo(id, type, disk, partGuid); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1101 | mVolumes.put(id, vol); |
| 1102 | onVolumeCreatedLocked(vol); |
| 1103 | break; |
| 1104 | } |
| 1105 | case VoldResponseCode.VOLUME_STATE_CHANGED: { |
| 1106 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1107 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1108 | if (vol != null) { |
| 1109 | final int oldState = vol.state; |
| 1110 | final int newState = Integer.parseInt(cooked[2]); |
| 1111 | vol.state = newState; |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1112 | onVolumeStateChangedLocked(vol, oldState, newState); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1113 | } |
| 1114 | break; |
| 1115 | } |
| 1116 | case VoldResponseCode.VOLUME_FS_TYPE_CHANGED: { |
| 1117 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1118 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1119 | if (vol != null) { |
| 1120 | vol.fsType = cooked[2]; |
| 1121 | } |
| 1122 | break; |
| 1123 | } |
| 1124 | case VoldResponseCode.VOLUME_FS_UUID_CHANGED: { |
| 1125 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1126 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1127 | if (vol != null) { |
| 1128 | vol.fsUuid = cooked[2]; |
| 1129 | } |
| 1130 | break; |
| 1131 | } |
| 1132 | case VoldResponseCode.VOLUME_FS_LABEL_CHANGED: { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1133 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1134 | if (vol != null) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 1135 | final StringBuilder builder = new StringBuilder(); |
| 1136 | for (int i = 2; i < cooked.length; i++) { |
| 1137 | builder.append(cooked[i]).append(' '); |
| 1138 | } |
| 1139 | vol.fsLabel = builder.toString().trim(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1140 | } |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1141 | // TODO: notify listeners that label changed |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1142 | break; |
| 1143 | } |
| 1144 | case VoldResponseCode.VOLUME_PATH_CHANGED: { |
| 1145 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1146 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1147 | if (vol != null) { |
| 1148 | vol.path = cooked[2]; |
| 1149 | } |
| 1150 | break; |
| 1151 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1152 | case VoldResponseCode.VOLUME_INTERNAL_PATH_CHANGED: { |
| 1153 | if (cooked.length != 3) break; |
| 1154 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
| 1155 | if (vol != null) { |
| 1156 | vol.internalPath = cooked[2]; |
| 1157 | } |
| 1158 | break; |
| 1159 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1160 | case VoldResponseCode.VOLUME_DESTROYED: { |
| 1161 | if (cooked.length != 2) break; |
| 1162 | mVolumes.remove(cooked[1]); |
| 1163 | break; |
| 1164 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1165 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1166 | case VoldResponseCode.MOVE_STATUS: { |
| 1167 | final int status = Integer.parseInt(cooked[1]); |
| 1168 | onMoveStatusLocked(status); |
| 1169 | break; |
| 1170 | } |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 1171 | case VoldResponseCode.BENCHMARK_RESULT: { |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1172 | if (cooked.length != 7) break; |
| 1173 | final String path = cooked[1]; |
| 1174 | final String ident = cooked[2]; |
| 1175 | final long create = Long.parseLong(cooked[3]); |
| 1176 | final long drop = Long.parseLong(cooked[4]); |
| 1177 | final long run = Long.parseLong(cooked[5]); |
| 1178 | final long destroy = Long.parseLong(cooked[6]); |
| 1179 | |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 1180 | final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1181 | dropBox.addText(TAG_STORAGE_BENCHMARK, scrubPath(path) |
| 1182 | + " " + ident + " " + create + " " + run + " " + destroy); |
| 1183 | |
| 1184 | final VolumeRecord rec = findRecordForPath(path); |
| 1185 | if (rec != null) { |
| 1186 | rec.lastBenchMillis = System.currentTimeMillis(); |
| 1187 | writeSettingsLocked(); |
| 1188 | } |
| 1189 | |
| 1190 | break; |
| 1191 | } |
| 1192 | case VoldResponseCode.TRIM_RESULT: { |
| 1193 | if (cooked.length != 4) break; |
| 1194 | final String path = cooked[1]; |
| 1195 | final long bytes = Long.parseLong(cooked[2]); |
| 1196 | final long time = Long.parseLong(cooked[3]); |
| 1197 | |
| 1198 | final DropBoxManager dropBox = mContext.getSystemService(DropBoxManager.class); |
| 1199 | dropBox.addText(TAG_STORAGE_TRIM, scrubPath(path) |
| 1200 | + " " + bytes + " " + time); |
| 1201 | |
| 1202 | final VolumeRecord rec = findRecordForPath(path); |
| 1203 | if (rec != null) { |
| 1204 | rec.lastTrimMillis = System.currentTimeMillis(); |
| 1205 | writeSettingsLocked(); |
| 1206 | } |
| 1207 | |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 1208 | break; |
| 1209 | } |
| 1210 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1211 | default: { |
| 1212 | Slog.d(TAG, "Unhandled vold event " + code); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 1213 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1214 | } |
| 1215 | |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 1216 | return true; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1219 | private void onDiskScannedLocked(DiskInfo disk) { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1220 | int volumeCount = 0; |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1221 | for (int i = 0; i < mVolumes.size(); i++) { |
| 1222 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 1223 | if (Objects.equals(disk.id, vol.getDiskId())) { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1224 | volumeCount++; |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1225 | } |
| 1226 | } |
| 1227 | |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1228 | final Intent intent = new Intent(DiskInfo.ACTION_DISK_SCANNED); |
| 1229 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1230 | intent.putExtra(DiskInfo.EXTRA_DISK_ID, disk.id); |
| 1231 | intent.putExtra(DiskInfo.EXTRA_VOLUME_COUNT, volumeCount); |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 1232 | mHandler.obtainMessage(H_INTERNAL_BROADCAST, intent).sendToTarget(); |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1233 | |
| 1234 | final CountDownLatch latch = mDiskScanLatches.remove(disk.id); |
| 1235 | if (latch != null) { |
| 1236 | latch.countDown(); |
| 1237 | } |
| 1238 | |
Jeff Sharkey | f5a6bd7 | 2015-05-19 14:42:38 -0700 | [diff] [blame] | 1239 | disk.volumeCount = volumeCount; |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1240 | mCallbacks.notifyDiskScanned(disk, volumeCount); |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1243 | private void onVolumeCreatedLocked(VolumeInfo vol) { |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1244 | if (vol.type == VolumeInfo.TYPE_EMULATED) { |
| 1245 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
| 1246 | final VolumeInfo privateVol = storage.findPrivateForEmulated(vol); |
| 1247 | |
| 1248 | if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid) |
| 1249 | && VolumeInfo.ID_PRIVATE_INTERNAL.equals(privateVol.id)) { |
| 1250 | Slog.v(TAG, "Found primary storage at " + vol); |
| 1251 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY; |
| 1252 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE; |
| 1253 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
| 1254 | |
| 1255 | } else if (Objects.equals(privateVol.fsUuid, mPrimaryStorageUuid)) { |
| 1256 | Slog.v(TAG, "Found primary storage at " + vol); |
| 1257 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY; |
| 1258 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE; |
| 1259 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
| 1260 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1261 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1262 | } else if (vol.type == VolumeInfo.TYPE_PUBLIC) { |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1263 | // TODO: only look at first public partition |
| 1264 | if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid) |
| 1265 | && vol.disk.isDefaultPrimary()) { |
| 1266 | Slog.v(TAG, "Found primary storage at " + vol); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 1267 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY; |
| 1268 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1269 | } |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 1270 | |
| 1271 | // Adoptable public disks are visible to apps, since they meet |
| 1272 | // public API requirement of being in a stable location. |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1273 | if (vol.disk.isAdoptable()) { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 1274 | vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE; |
| 1275 | } |
| 1276 | |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 1277 | vol.mountUserId = ActivityManager.getCurrentUser(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1278 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1279 | |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 1280 | } else if (vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 1281 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
| 1282 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1283 | } else { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1284 | Slog.d(TAG, "Skipping automatic mounting of " + vol); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1288 | private boolean isBroadcastWorthy(VolumeInfo vol) { |
| 1289 | switch (vol.getType()) { |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1290 | case VolumeInfo.TYPE_PRIVATE: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1291 | case VolumeInfo.TYPE_PUBLIC: |
| 1292 | case VolumeInfo.TYPE_EMULATED: |
| 1293 | break; |
| 1294 | default: |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
| 1298 | switch (vol.getState()) { |
| 1299 | case VolumeInfo.STATE_MOUNTED: |
| 1300 | case VolumeInfo.STATE_MOUNTED_READ_ONLY: |
| 1301 | case VolumeInfo.STATE_EJECTING: |
| 1302 | case VolumeInfo.STATE_UNMOUNTED: |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1303 | case VolumeInfo.STATE_UNMOUNTABLE: |
Tony Mantler | f0d7105 | 2015-06-24 11:45:25 -0700 | [diff] [blame] | 1304 | case VolumeInfo.STATE_BAD_REMOVAL: |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1305 | break; |
| 1306 | default: |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
| 1310 | return true; |
| 1311 | } |
| 1312 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1313 | private void onVolumeStateChangedLocked(VolumeInfo vol, int oldState, int newState) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1314 | // Remember that we saw this volume so we're ready to accept user |
| 1315 | // metadata, or so we can annoy them when a private volume is ejected |
| 1316 | if (vol.isMountedReadable() && !TextUtils.isEmpty(vol.fsUuid)) { |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1317 | VolumeRecord rec = mRecords.get(vol.fsUuid); |
| 1318 | if (rec == null) { |
| 1319 | rec = new VolumeRecord(vol.type, vol.fsUuid); |
| 1320 | rec.partGuid = vol.partGuid; |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1321 | rec.createdMillis = System.currentTimeMillis(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1322 | if (vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 1323 | rec.nickname = vol.disk.getDescription(); |
| 1324 | } |
| 1325 | mRecords.put(rec.fsUuid, rec); |
| 1326 | writeSettingsLocked(); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1327 | } else { |
| 1328 | // Handle upgrade case where we didn't store partition GUID |
| 1329 | if (TextUtils.isEmpty(rec.partGuid)) { |
| 1330 | rec.partGuid = vol.partGuid; |
| 1331 | writeSettingsLocked(); |
| 1332 | } |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1336 | mCallbacks.notifyVolumeStateChanged(vol, oldState, newState); |
| 1337 | |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 1338 | // Do not broadcast before boot has completed to avoid launching the |
| 1339 | // processes that receive the intent unnecessarily. |
| 1340 | if (mBootCompleted && isBroadcastWorthy(vol)) { |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1341 | final Intent intent = new Intent(VolumeInfo.ACTION_VOLUME_STATE_CHANGED); |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 1342 | intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, vol.id); |
| 1343 | intent.putExtra(VolumeInfo.EXTRA_VOLUME_STATE, newState); |
Tony Mantler | f0d7105 | 2015-06-24 11:45:25 -0700 | [diff] [blame] | 1344 | intent.putExtra(VolumeRecord.EXTRA_FS_UUID, vol.fsUuid); |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1345 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Jeff Sharkey | abc3e85 | 2015-08-03 14:41:13 -0700 | [diff] [blame] | 1346 | mHandler.obtainMessage(H_INTERNAL_BROADCAST, intent).sendToTarget(); |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 1347 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1348 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1349 | final String oldStateEnv = VolumeInfo.getEnvironmentForState(oldState); |
| 1350 | final String newStateEnv = VolumeInfo.getEnvironmentForState(newState); |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 1351 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1352 | if (!Objects.equals(oldStateEnv, newStateEnv)) { |
| 1353 | // Kick state changed event towards all started users. Any users |
| 1354 | // started after this point will trigger additional |
| 1355 | // user-specific broadcasts. |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1356 | for (int userId : mSystemUnlockedUsers) { |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 1357 | if (vol.isVisibleForRead(userId)) { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1358 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, false); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1359 | mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1360 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1361 | mCallbacks.notifyStorageStateChanged(userVol.getPath(), oldStateEnv, |
| 1362 | newStateEnv); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1366 | |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 1367 | if (vol.type == VolumeInfo.TYPE_PUBLIC && vol.state == VolumeInfo.STATE_EJECTING) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1368 | // TODO: this should eventually be handled by new ObbVolume state changes |
| 1369 | /* |
| 1370 | * Some OBBs might have been unmounted when this volume was |
| 1371 | * unmounted, so send a message to the handler to let it know to |
| 1372 | * remove those from the list of mounted OBBS. |
| 1373 | */ |
| 1374 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage( |
| 1375 | OBB_FLUSH_MOUNT_STATE, vol.path)); |
| 1376 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1379 | private void onMoveStatusLocked(int status) { |
| 1380 | if (mMoveCallback == null) { |
| 1381 | Slog.w(TAG, "Odd, status but no move requested"); |
| 1382 | return; |
| 1383 | } |
| 1384 | |
| 1385 | // TODO: estimate remaining time |
| 1386 | try { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1387 | mMoveCallback.onStatusChanged(-1, status, -1); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1388 | } catch (RemoteException ignored) { |
| 1389 | } |
| 1390 | |
| 1391 | // We've finished copying and we're about to clean up old data, so |
| 1392 | // remember that move was successful if we get rebooted |
| 1393 | if (status == MOVE_STATUS_COPY_FINISHED) { |
| 1394 | Slog.d(TAG, "Move to " + mMoveTargetUuid + " copy phase finshed; persisting"); |
| 1395 | |
| 1396 | mPrimaryStorageUuid = mMoveTargetUuid; |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1397 | writeSettingsLocked(); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | if (PackageManager.isMoveStatusFinished(status)) { |
| 1401 | Slog.d(TAG, "Move to " + mMoveTargetUuid + " finished with status " + status); |
| 1402 | |
| 1403 | mMoveCallback = null; |
| 1404 | mMoveTargetUuid = null; |
| 1405 | } |
| 1406 | } |
| 1407 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1408 | private void enforcePermission(String perm) { |
| 1409 | mContext.enforceCallingOrSelfPermission(perm, perm); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 1410 | } |
| 1411 | |
Jeff Sharkey | 2e606d7 | 2015-07-27 14:19:54 -0700 | [diff] [blame] | 1412 | /** |
| 1413 | * Decide if volume is mountable per device policies. |
| 1414 | */ |
| 1415 | private boolean isMountDisallowed(VolumeInfo vol) { |
| 1416 | if (vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 1417 | final UserManager userManager = mContext.getSystemService(UserManager.class); |
| 1418 | return userManager.hasUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, |
| 1419 | Binder.getCallingUserHandle()); |
| 1420 | } else { |
| 1421 | return false; |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
Amith Yamasani | 462ac3a | 2015-06-30 14:21:01 -0700 | [diff] [blame] | 1425 | private void enforceAdminUser() { |
| 1426 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 1427 | final int callingUserId = UserHandle.getCallingUserId(); |
| 1428 | boolean isAdmin; |
| 1429 | long token = Binder.clearCallingIdentity(); |
| 1430 | try { |
| 1431 | isAdmin = um.getUserInfo(callingUserId).isAdmin(); |
| 1432 | } finally { |
| 1433 | Binder.restoreCallingIdentity(token); |
| 1434 | } |
| 1435 | if (!isAdmin) { |
| 1436 | throw new SecurityException("Only admin users can adopt sd cards"); |
| 1437 | } |
| 1438 | } |
| 1439 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1440 | /** |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1441 | * Constructs a new MountService instance |
| 1442 | * |
| 1443 | * @param context Binder context for this service |
| 1444 | */ |
| 1445 | public MountService(Context context) { |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 1446 | sSelf = this; |
| 1447 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1448 | mContext = context; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1449 | mCallbacks = new Callbacks(FgThread.get().getLooper()); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1450 | mLockPatternUtils = new LockPatternUtils(mContext); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1451 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1452 | // XXX: This will go away soon in favor of IMountServiceObserver |
| 1453 | mPms = (PackageManagerService) ServiceManager.getService("package"); |
| 1454 | |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 1455 | HandlerThread hthread = new HandlerThread(TAG); |
| 1456 | hthread.start(); |
| 1457 | mHandler = new MountServiceHandler(hthread.getLooper()); |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 1458 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1459 | // Add OBB Action Handler to MountService thread. |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 1460 | mObbActionHandler = new ObbActionHandler(IoThread.get().getLooper()); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1461 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 1462 | // Initialize the last-fstrim tracking if necessary |
| 1463 | File dataDir = Environment.getDataDirectory(); |
| 1464 | File systemDir = new File(dataDir, "system"); |
| 1465 | mLastMaintenanceFile = new File(systemDir, LAST_FSTRIM_FILE); |
| 1466 | if (!mLastMaintenanceFile.exists()) { |
| 1467 | // Not setting mLastMaintenance here means that we will force an |
| 1468 | // fstrim during reboot following the OTA that installs this code. |
| 1469 | try { |
| 1470 | (new FileOutputStream(mLastMaintenanceFile)).close(); |
| 1471 | } catch (IOException e) { |
| 1472 | Slog.e(TAG, "Unable to create fstrim record " + mLastMaintenanceFile.getPath()); |
| 1473 | } |
| 1474 | } else { |
| 1475 | mLastMaintenance = mLastMaintenanceFile.lastModified(); |
| 1476 | } |
| 1477 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1478 | mSettingsFile = new AtomicFile( |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 1479 | new File(Environment.getDataSystemDirectory(), "storage.xml")); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1480 | |
| 1481 | synchronized (mLock) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1482 | readSettingsLocked(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1485 | LocalServices.addService(MountServiceInternal.class, mMountServiceInternal); |
| 1486 | |
Marco Nelissen | c34ebce | 2010-02-18 13:39:41 -0800 | [diff] [blame] | 1487 | /* |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 1488 | * Create the connection to vold with a maximum queue of twice the |
| 1489 | * amount of containers we'd ever expect to have. This keeps an |
| 1490 | * "asec list" from blocking a thread repeatedly. |
| 1491 | */ |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 1492 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1493 | mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG, 25, |
| 1494 | null); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1495 | mConnector.setDebug(true); |
Jeff Sharkey | 8948c01 | 2015-11-03 12:33:54 -0800 | [diff] [blame] | 1496 | mConnector.setWarnIfHeld(mLock); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 1497 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 1498 | Thread thread = new Thread(mConnector, VOLD_TAG); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1499 | thread.start(); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 1500 | |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 1501 | // Reuse parameters from first connector since they are tested and safe |
| 1502 | mCryptConnector = new NativeDaemonConnector(this, "cryptd", |
| 1503 | MAX_CONTAINERS * 2, CRYPTD_TAG, 25, null); |
| 1504 | mCryptConnector.setDebug(true); |
| 1505 | |
| 1506 | Thread crypt_thread = new Thread(mCryptConnector, CRYPTD_TAG); |
| 1507 | crypt_thread.start(); |
| 1508 | |
Jeff Sharkey | bcd262d | 2015-06-10 09:41:17 -0700 | [diff] [blame] | 1509 | final IntentFilter userFilter = new IntentFilter(); |
| 1510 | userFilter.addAction(Intent.ACTION_USER_ADDED); |
| 1511 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 1512 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 1513 | |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1514 | synchronized (mLock) { |
| 1515 | addInternalVolumeLocked(); |
| 1516 | } |
Amith Yamasani | a789248 | 2015-08-07 11:09:05 -0700 | [diff] [blame] | 1517 | |
Kenny Root | 07714d4 | 2011-08-17 17:49:28 -0700 | [diff] [blame] | 1518 | // Add ourself to the Watchdog monitors if enabled. |
| 1519 | if (WATCHDOG_ENABLE) { |
| 1520 | Watchdog.getInstance().addMonitor(this); |
| 1521 | } |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1522 | } |
| 1523 | |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 1524 | private void systemReady() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1525 | mSystemReady = true; |
| 1526 | mHandler.obtainMessage(H_SYSTEM_READY).sendToTarget(); |
| 1527 | } |
| 1528 | |
Yasuhiro Matsuda | 87a38b5 | 2015-07-24 22:10:16 +0900 | [diff] [blame] | 1529 | private void bootCompleted() { |
| 1530 | mBootCompleted = true; |
| 1531 | } |
| 1532 | |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1533 | private String getDefaultPrimaryStorageUuid() { |
| 1534 | if (SystemProperties.getBoolean(StorageManager.PROP_PRIMARY_PHYSICAL, false)) { |
| 1535 | return StorageManager.UUID_PRIMARY_PHYSICAL; |
| 1536 | } else { |
| 1537 | return StorageManager.UUID_PRIVATE_INTERNAL; |
| 1538 | } |
| 1539 | } |
| 1540 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1541 | private void readSettingsLocked() { |
| 1542 | mRecords.clear(); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1543 | mPrimaryStorageUuid = getDefaultPrimaryStorageUuid(); |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 1544 | mForceAdoptable = false; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1545 | |
| 1546 | FileInputStream fis = null; |
| 1547 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1548 | fis = mSettingsFile.openRead(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1549 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1550 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1551 | |
| 1552 | int type; |
| 1553 | while ((type = in.next()) != END_DOCUMENT) { |
| 1554 | if (type == START_TAG) { |
| 1555 | final String tag = in.getName(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1556 | if (TAG_VOLUMES.equals(tag)) { |
| 1557 | final int version = readIntAttribute(in, ATTR_VERSION, VERSION_INIT); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1558 | final boolean primaryPhysical = SystemProperties.getBoolean( |
| 1559 | StorageManager.PROP_PRIMARY_PHYSICAL, false); |
| 1560 | final boolean validAttr = (version >= VERSION_FIX_PRIMARY) |
| 1561 | || (version >= VERSION_ADD_PRIMARY && !primaryPhysical); |
| 1562 | if (validAttr) { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1563 | mPrimaryStorageUuid = readStringAttribute(in, |
| 1564 | ATTR_PRIMARY_STORAGE_UUID); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1565 | } |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 1566 | mForceAdoptable = readBooleanAttribute(in, ATTR_FORCE_ADOPTABLE, false); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1567 | |
| 1568 | } else if (TAG_VOLUME.equals(tag)) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1569 | final VolumeRecord rec = readVolumeRecord(in); |
| 1570 | mRecords.put(rec.fsUuid, rec); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1571 | } |
| 1572 | } |
| 1573 | } |
| 1574 | } catch (FileNotFoundException e) { |
| 1575 | // Missing metadata is okay, probably first boot |
| 1576 | } catch (IOException e) { |
| 1577 | Slog.wtf(TAG, "Failed reading metadata", e); |
| 1578 | } catch (XmlPullParserException e) { |
| 1579 | Slog.wtf(TAG, "Failed reading metadata", e); |
| 1580 | } finally { |
| 1581 | IoUtils.closeQuietly(fis); |
| 1582 | } |
| 1583 | } |
| 1584 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1585 | private void writeSettingsLocked() { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1586 | FileOutputStream fos = null; |
| 1587 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1588 | fos = mSettingsFile.startWrite(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1589 | |
| 1590 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1591 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1592 | out.startDocument(null, true); |
| 1593 | out.startTag(null, TAG_VOLUMES); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1594 | writeIntAttribute(out, ATTR_VERSION, VERSION_FIX_PRIMARY); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1595 | writeStringAttribute(out, ATTR_PRIMARY_STORAGE_UUID, mPrimaryStorageUuid); |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 1596 | writeBooleanAttribute(out, ATTR_FORCE_ADOPTABLE, mForceAdoptable); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1597 | final int size = mRecords.size(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1598 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1599 | final VolumeRecord rec = mRecords.valueAt(i); |
| 1600 | writeVolumeRecord(out, rec); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1601 | } |
| 1602 | out.endTag(null, TAG_VOLUMES); |
| 1603 | out.endDocument(); |
| 1604 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1605 | mSettingsFile.finishWrite(fos); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1606 | } catch (IOException e) { |
| 1607 | if (fos != null) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1608 | mSettingsFile.failWrite(fos); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1613 | public static VolumeRecord readVolumeRecord(XmlPullParser in) throws IOException { |
| 1614 | final int type = readIntAttribute(in, ATTR_TYPE); |
| 1615 | final String fsUuid = readStringAttribute(in, ATTR_FS_UUID); |
| 1616 | final VolumeRecord meta = new VolumeRecord(type, fsUuid); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1617 | meta.partGuid = readStringAttribute(in, ATTR_PART_GUID); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1618 | meta.nickname = readStringAttribute(in, ATTR_NICKNAME); |
| 1619 | meta.userFlags = readIntAttribute(in, ATTR_USER_FLAGS); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1620 | meta.createdMillis = readLongAttribute(in, ATTR_CREATED_MILLIS); |
| 1621 | meta.lastTrimMillis = readLongAttribute(in, ATTR_LAST_TRIM_MILLIS); |
| 1622 | meta.lastBenchMillis = readLongAttribute(in, ATTR_LAST_BENCH_MILLIS); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1623 | return meta; |
| 1624 | } |
| 1625 | |
| 1626 | public static void writeVolumeRecord(XmlSerializer out, VolumeRecord rec) throws IOException { |
| 1627 | out.startTag(null, TAG_VOLUME); |
| 1628 | writeIntAttribute(out, ATTR_TYPE, rec.type); |
| 1629 | writeStringAttribute(out, ATTR_FS_UUID, rec.fsUuid); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1630 | writeStringAttribute(out, ATTR_PART_GUID, rec.partGuid); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1631 | writeStringAttribute(out, ATTR_NICKNAME, rec.nickname); |
| 1632 | writeIntAttribute(out, ATTR_USER_FLAGS, rec.userFlags); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 1633 | writeLongAttribute(out, ATTR_CREATED_MILLIS, rec.createdMillis); |
| 1634 | writeLongAttribute(out, ATTR_LAST_TRIM_MILLIS, rec.lastTrimMillis); |
| 1635 | writeLongAttribute(out, ATTR_LAST_BENCH_MILLIS, rec.lastBenchMillis); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1636 | out.endTag(null, TAG_VOLUME); |
| 1637 | } |
| 1638 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1639 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1640 | * Exposed API calls below here |
| 1641 | */ |
| 1642 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1643 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1644 | public void registerListener(IMountServiceListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1645 | mCallbacks.register(listener); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1646 | } |
| 1647 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1648 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1649 | public void unregisterListener(IMountServiceListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1650 | mCallbacks.unregister(listener); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1651 | } |
| 1652 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1653 | @Override |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 1654 | public void shutdown(final IMountShutdownObserver observer) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1655 | enforcePermission(android.Manifest.permission.SHUTDOWN); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1656 | |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1657 | Slog.i(TAG, "Shutting down"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1658 | mHandler.obtainMessage(H_SHUTDOWN, observer).sendToTarget(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1661 | @Override |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1662 | public boolean isUsbMassStorageConnected() { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1663 | throw new UnsupportedOperationException(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1666 | @Override |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1667 | public void setUsbMassStorageEnabled(boolean enable) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1668 | throw new UnsupportedOperationException(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1671 | @Override |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1672 | public boolean isUsbMassStorageEnabled() { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1673 | throw new UnsupportedOperationException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1674 | } |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1675 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1676 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1677 | public String getVolumeState(String mountPoint) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1678 | throw new UnsupportedOperationException(); |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1679 | } |
| 1680 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1681 | @Override |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1682 | public boolean isExternalStorageEmulated() { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1683 | throw new UnsupportedOperationException(); |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1686 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1687 | public int mountVolume(String path) { |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 1688 | mount(findVolumeIdForPathOrThrow(path)); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1689 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | } |
| 1691 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1692 | @Override |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 1693 | public void unmountVolume(String path, boolean force, boolean removeEncryption) { |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 1694 | unmount(findVolumeIdForPathOrThrow(path)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1695 | } |
| 1696 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1697 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1698 | public int formatVolume(String path) { |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 1699 | format(findVolumeIdForPathOrThrow(path)); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | @Override |
| 1704 | public void mount(String volId) { |
| 1705 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1706 | waitForReady(); |
| 1707 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 1708 | final VolumeInfo vol = findVolumeByIdOrThrow(volId); |
Jeff Sharkey | 2e606d7 | 2015-07-27 14:19:54 -0700 | [diff] [blame] | 1709 | if (isMountDisallowed(vol)) { |
| 1710 | throw new SecurityException("Mounting " + volId + " restricted by policy"); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1711 | } |
| 1712 | try { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 1713 | mConnector.execute("volume", "mount", vol.id, vol.mountFlags, vol.mountUserId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1714 | } catch (NativeDaemonConnectorException e) { |
| 1715 | throw e.rethrowAsParcelableException(); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | @Override |
| 1720 | public void unmount(String volId) { |
| 1721 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1722 | waitForReady(); |
| 1723 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 1724 | final VolumeInfo vol = findVolumeByIdOrThrow(volId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1725 | |
| 1726 | // TODO: expand PMS to know about multiple volumes |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1727 | if (vol.isPrimaryPhysical()) { |
| 1728 | final long ident = Binder.clearCallingIdentity(); |
| 1729 | try { |
| 1730 | synchronized (mUnmountLock) { |
| 1731 | mUnmountSignal = new CountDownLatch(1); |
| 1732 | mPms.updateExternalMediaStatus(false, true); |
| 1733 | waitForLatch(mUnmountSignal, "mUnmountSignal"); |
| 1734 | mUnmountSignal = null; |
| 1735 | } |
| 1736 | } finally { |
| 1737 | Binder.restoreCallingIdentity(ident); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | try { |
| 1742 | mConnector.execute("volume", "unmount", vol.id); |
| 1743 | } catch (NativeDaemonConnectorException e) { |
| 1744 | throw e.rethrowAsParcelableException(); |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | @Override |
| 1749 | public void format(String volId) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1750 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1751 | waitForReady(); |
San Mehat | 5b77dab | 2010-01-26 13:28:50 -0800 | [diff] [blame] | 1752 | |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 1753 | final VolumeInfo vol = findVolumeByIdOrThrow(volId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1754 | try { |
Jeff Sharkey | 4e83cc9 | 2015-05-27 14:38:39 -0700 | [diff] [blame] | 1755 | mConnector.execute("volume", "format", vol.id, "auto"); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1756 | } catch (NativeDaemonConnectorException e) { |
| 1757 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1758 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | @Override |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 1762 | public long benchmark(String volId) { |
| 1763 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| 1764 | waitForReady(); |
| 1765 | |
| 1766 | try { |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 1767 | // TODO: make benchmark async so we don't block other commands |
| 1768 | final NativeDaemonEvent res = mConnector.execute(3 * DateUtils.MINUTE_IN_MILLIS, |
| 1769 | "volume", "benchmark", volId); |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 1770 | return Long.parseLong(res.getMessage()); |
Todd Kennedy | 8101ee6 | 2015-06-23 13:35:28 -0700 | [diff] [blame] | 1771 | } catch (NativeDaemonTimeoutException e) { |
| 1772 | return Long.MAX_VALUE; |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 1773 | } catch (NativeDaemonConnectorException e) { |
| 1774 | throw e.rethrowAsParcelableException(); |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | @Override |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1779 | public void partitionPublic(String diskId) { |
| 1780 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| 1781 | waitForReady(); |
| 1782 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1783 | final CountDownLatch latch = findOrCreateDiskScanLatch(diskId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1784 | try { |
| 1785 | mConnector.execute("volume", "partition", diskId, "public"); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 1786 | waitForLatch(latch, "partitionPublic", 3 * DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1787 | } catch (NativeDaemonConnectorException e) { |
| 1788 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 1789 | } catch (TimeoutException e) { |
| 1790 | throw new IllegalStateException(e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | @Override |
| 1795 | public void partitionPrivate(String diskId) { |
| 1796 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Amith Yamasani | 462ac3a | 2015-06-30 14:21:01 -0700 | [diff] [blame] | 1797 | enforceAdminUser(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1798 | waitForReady(); |
| 1799 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1800 | final CountDownLatch latch = findOrCreateDiskScanLatch(diskId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1801 | try { |
| 1802 | mConnector.execute("volume", "partition", diskId, "private"); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 1803 | waitForLatch(latch, "partitionPrivate", 3 * DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1804 | } catch (NativeDaemonConnectorException e) { |
| 1805 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 1806 | } catch (TimeoutException e) { |
| 1807 | throw new IllegalStateException(e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | @Override |
| 1812 | public void partitionMixed(String diskId, int ratio) { |
| 1813 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
Amith Yamasani | 462ac3a | 2015-06-30 14:21:01 -0700 | [diff] [blame] | 1814 | enforceAdminUser(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1815 | waitForReady(); |
| 1816 | |
Jeff Sharkey | eba260d | 2015-04-19 14:35:16 -0700 | [diff] [blame] | 1817 | final CountDownLatch latch = findOrCreateDiskScanLatch(diskId); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1818 | try { |
| 1819 | mConnector.execute("volume", "partition", diskId, "mixed", ratio); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 1820 | waitForLatch(latch, "partitionMixed", 3 * DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1821 | } catch (NativeDaemonConnectorException e) { |
| 1822 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | edcdaf6 | 2015-07-09 09:45:36 -0700 | [diff] [blame] | 1823 | } catch (TimeoutException e) { |
| 1824 | throw new IllegalStateException(e); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1825 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1828 | @Override |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1829 | public void setVolumeNickname(String fsUuid, String nickname) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1830 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1831 | waitForReady(); |
| 1832 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1833 | Preconditions.checkNotNull(fsUuid); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1834 | synchronized (mLock) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1835 | final VolumeRecord rec = mRecords.get(fsUuid); |
| 1836 | rec.nickname = nickname; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1837 | mCallbacks.notifyVolumeRecordChanged(rec); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1838 | writeSettingsLocked(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | @Override |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1843 | public void setVolumeUserFlags(String fsUuid, int flags, int mask) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1844 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1845 | waitForReady(); |
| 1846 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1847 | Preconditions.checkNotNull(fsUuid); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1848 | synchronized (mLock) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1849 | final VolumeRecord rec = mRecords.get(fsUuid); |
| 1850 | rec.userFlags = (rec.userFlags & ~mask) | (flags & mask); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1851 | mCallbacks.notifyVolumeRecordChanged(rec); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1852 | writeSettingsLocked(); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | @Override |
| 1857 | public void forgetVolume(String fsUuid) { |
| 1858 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1859 | waitForReady(); |
| 1860 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1861 | Preconditions.checkNotNull(fsUuid); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1862 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1863 | synchronized (mLock) { |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1864 | final VolumeRecord rec = mRecords.remove(fsUuid); |
| 1865 | if (rec != null && !TextUtils.isEmpty(rec.partGuid)) { |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1866 | mHandler.obtainMessage(H_PARTITION_FORGET, rec.partGuid).sendToTarget(); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1867 | } |
| 1868 | mCallbacks.notifyVolumeForgotten(fsUuid); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1869 | |
| 1870 | // If this had been primary storage, revert back to internal and |
| 1871 | // reset vold so we bind into new volume into place. |
| 1872 | if (Objects.equals(mPrimaryStorageUuid, fsUuid)) { |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1873 | mPrimaryStorageUuid = getDefaultPrimaryStorageUuid(); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1874 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 1877 | writeSettingsLocked(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | |
Jeff Sharkey | 7d2af54 | 2015-05-12 15:27:15 -0700 | [diff] [blame] | 1881 | @Override |
| 1882 | public void forgetAllVolumes() { |
| 1883 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1884 | waitForReady(); |
| 1885 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1886 | synchronized (mLock) { |
| 1887 | for (int i = 0; i < mRecords.size(); i++) { |
| 1888 | final String fsUuid = mRecords.keyAt(i); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1889 | final VolumeRecord rec = mRecords.valueAt(i); |
| 1890 | if (!TextUtils.isEmpty(rec.partGuid)) { |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1891 | mHandler.obtainMessage(H_PARTITION_FORGET, rec.partGuid).sendToTarget(); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1892 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1893 | mCallbacks.notifyVolumeForgotten(fsUuid); |
| 1894 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1895 | mRecords.clear(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1896 | |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1897 | if (!Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid)) { |
| 1898 | mPrimaryStorageUuid = getDefaultPrimaryStorageUuid(); |
| 1899 | } |
| 1900 | |
| 1901 | writeSettingsLocked(); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 1902 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 1906 | private void forgetPartition(String partGuid) { |
| 1907 | try { |
| 1908 | mConnector.execute("volume", "forget_partition", partGuid); |
| 1909 | } catch (NativeDaemonConnectorException e) { |
| 1910 | Slog.w(TAG, "Failed to forget key for " + partGuid + ": " + e); |
| 1911 | } |
| 1912 | } |
| 1913 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1914 | private void remountUidExternalStorage(int uid, int mode) { |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 1915 | waitForReady(); |
| 1916 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1917 | String modeName = "none"; |
| 1918 | switch (mode) { |
| 1919 | case Zygote.MOUNT_EXTERNAL_DEFAULT: { |
| 1920 | modeName = "default"; |
| 1921 | } break; |
| 1922 | |
| 1923 | case Zygote.MOUNT_EXTERNAL_READ: { |
| 1924 | modeName = "read"; |
| 1925 | } break; |
| 1926 | |
| 1927 | case Zygote.MOUNT_EXTERNAL_WRITE: { |
| 1928 | modeName = "write"; |
| 1929 | } break; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | try { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1933 | mConnector.execute("volume", "remount_uid", uid, modeName); |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 1934 | } catch (NativeDaemonConnectorException e) { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1935 | Slog.w(TAG, "Failed to remount UID " + uid + " as " + modeName + ": " + e); |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | @Override |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 1940 | public void setDebugFlags(int flags, int mask) { |
| 1941 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1942 | waitForReady(); |
| 1943 | |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 1944 | if ((mask & StorageManager.DEBUG_EMULATE_FBE) != 0) { |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 1945 | if (StorageManager.isFileEncryptedNativeOnly()) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1946 | throw new IllegalStateException( |
| 1947 | "Emulation not available on device with native FBE"); |
| 1948 | } |
Jeff Sharkey | 5a78516 | 2016-03-21 13:02:06 -0600 | [diff] [blame] | 1949 | if (mLockPatternUtils.isCredentialRequiredToDecrypt(false)) { |
| 1950 | throw new IllegalStateException( |
| 1951 | "Emulation requires disabling 'Secure start-up' in Settings > Security"); |
| 1952 | } |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1953 | |
Jeff Sharkey | 1176e51 | 2016-02-29 17:01:26 -0700 | [diff] [blame] | 1954 | final long token = Binder.clearCallingIdentity(); |
| 1955 | try { |
| 1956 | final boolean emulateFbe = (flags & StorageManager.DEBUG_EMULATE_FBE) != 0; |
| 1957 | SystemProperties.set(StorageManager.PROP_EMULATE_FBE, Boolean.toString(emulateFbe)); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1958 | |
Jeff Sharkey | 1176e51 | 2016-02-29 17:01:26 -0700 | [diff] [blame] | 1959 | // Perform hard reboot to kick policy into place |
| 1960 | mContext.getSystemService(PowerManager.class).reboot(null); |
| 1961 | } finally { |
| 1962 | Binder.restoreCallingIdentity(token); |
| 1963 | } |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 1964 | } |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 1965 | |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 1966 | if ((mask & StorageManager.DEBUG_FORCE_ADOPTABLE) != 0) { |
| 1967 | synchronized (mLock) { |
| 1968 | mForceAdoptable = (flags & StorageManager.DEBUG_FORCE_ADOPTABLE) != 0; |
| 1969 | |
| 1970 | writeSettingsLocked(); |
| 1971 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
| 1972 | } |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | @Override |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1977 | public String getPrimaryStorageUuid() { |
| 1978 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1979 | waitForReady(); |
| 1980 | |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1981 | synchronized (mLock) { |
| 1982 | return mPrimaryStorageUuid; |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | @Override |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1987 | public void setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback) { |
| 1988 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1989 | waitForReady(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 1990 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1991 | synchronized (mLock) { |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 1992 | if (Objects.equals(mPrimaryStorageUuid, volumeUuid)) { |
| 1993 | throw new IllegalArgumentException("Primary storage already at " + volumeUuid); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | if (mMoveCallback != null) { |
| 1997 | throw new IllegalStateException("Move already in progress"); |
| 1998 | } |
| 1999 | mMoveCallback = callback; |
| 2000 | mMoveTargetUuid = volumeUuid; |
| 2001 | |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2002 | // When moving to/from primary physical volume, we probably just nuked |
| 2003 | // the current storage location, so we have nothing to move. |
| 2004 | if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid) |
| 2005 | || Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) { |
| 2006 | Slog.d(TAG, "Skipping move to/from primary physical"); |
| 2007 | onMoveStatusLocked(MOVE_STATUS_COPY_FINISHED); |
| 2008 | onMoveStatusLocked(PackageManager.MOVE_SUCCEEDED); |
Jeff Sharkey | 5a9bb74 | 2015-11-03 10:15:57 -0800 | [diff] [blame] | 2009 | mHandler.obtainMessage(H_RESET).sendToTarget(); |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2010 | |
| 2011 | } else { |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 2012 | final VolumeInfo from = findStorageForUuid(mPrimaryStorageUuid); |
| 2013 | final VolumeInfo to = findStorageForUuid(volumeUuid); |
| 2014 | |
| 2015 | if (from == null) { |
| 2016 | Slog.w(TAG, "Failing move due to missing from volume " + mPrimaryStorageUuid); |
| 2017 | onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR); |
| 2018 | return; |
| 2019 | } else if (to == null) { |
| 2020 | Slog.w(TAG, "Failing move due to missing to volume " + volumeUuid); |
| 2021 | onMoveStatusLocked(PackageManager.MOVE_FAILED_INTERNAL_ERROR); |
| 2022 | return; |
| 2023 | } |
Jeff Sharkey | fced534 | 2015-05-10 14:53:34 -0700 | [diff] [blame] | 2024 | |
| 2025 | try { |
| 2026 | mConnector.execute("volume", "move_storage", from.id, to.id); |
| 2027 | } catch (NativeDaemonConnectorException e) { |
| 2028 | throw e.rethrowAsParcelableException(); |
| 2029 | } |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 2030 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | @Override |
Mike Lockwood | ecedfdc | 2011-06-08 15:11:59 -0700 | [diff] [blame] | 2035 | public int[] getStorageUsers(String path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2036 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 2037 | waitForReady(); |
| 2038 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2039 | final String[] r = NativeDaemonEvent.filterMessageList( |
| 2040 | mConnector.executeForList("storage", "users", path), |
| 2041 | VoldResponseCode.StorageUsersListResult); |
| 2042 | |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 2043 | // FMT: <pid> <process name> |
| 2044 | int[] data = new int[r.length]; |
| 2045 | for (int i = 0; i < r.length; i++) { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2046 | String[] tok = r[i].split(" "); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 2047 | try { |
| 2048 | data[i] = Integer.parseInt(tok[0]); |
| 2049 | } catch (NumberFormatException nfe) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 2050 | Slog.e(TAG, String.format("Error parsing pid %s", tok[0])); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 2051 | return new int[0]; |
| 2052 | } |
| 2053 | } |
| 2054 | return data; |
| 2055 | } catch (NativeDaemonConnectorException e) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 2056 | Slog.e(TAG, "Failed to retrieve storage users list", e); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 2057 | return new int[0]; |
| 2058 | } |
| 2059 | } |
| 2060 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2061 | private void warnOnNotMounted() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2062 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2063 | for (int i = 0; i < mVolumes.size(); i++) { |
| 2064 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 2065 | if (vol.isPrimary() && vol.isMountedWritable()) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2066 | // Cool beans, we have a mounted primary volume |
| 2067 | return; |
| 2068 | } |
Jeff Sharkey | 32ee831 | 2012-09-30 13:21:31 -0700 | [diff] [blame] | 2069 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2070 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2071 | |
| 2072 | Slog.w(TAG, "No primary storage mounted!"); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2073 | } |
| 2074 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2075 | public String[] getSecureContainerList() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2076 | enforcePermission(android.Manifest.permission.ASEC_ACCESS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 2077 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2078 | warnOnNotMounted(); |
San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 2079 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2080 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2081 | return NativeDaemonEvent.filterMessageList( |
| 2082 | mConnector.executeForList("asec", "list"), VoldResponseCode.AsecListResult); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2083 | } catch (NativeDaemonConnectorException e) { |
| 2084 | return new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2085 | } |
| 2086 | } |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 2087 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 2088 | public int createSecureContainer(String id, int sizeMb, String fstype, String key, |
| 2089 | int ownerUid, boolean external) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2090 | enforcePermission(android.Manifest.permission.ASEC_CREATE); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 2091 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2092 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2093 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2094 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2095 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 2096 | mConnector.execute("asec", "create", id, sizeMb, fstype, new SensitiveArg(key), |
| 2097 | ownerUid, external ? "1" : "0"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2098 | } catch (NativeDaemonConnectorException e) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2099 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 2100 | } |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 2101 | |
| 2102 | if (rc == StorageResultCode.OperationSucceeded) { |
| 2103 | synchronized (mAsecMountSet) { |
| 2104 | mAsecMountSet.add(id); |
| 2105 | } |
| 2106 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2107 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 2108 | } |
| 2109 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2110 | @Override |
| 2111 | public int resizeSecureContainer(String id, int sizeMb, String key) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2112 | enforcePermission(android.Manifest.permission.ASEC_CREATE); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2113 | waitForReady(); |
| 2114 | warnOnNotMounted(); |
| 2115 | |
| 2116 | int rc = StorageResultCode.OperationSucceeded; |
| 2117 | try { |
| 2118 | mConnector.execute("asec", "resize", id, sizeMb, new SensitiveArg(key)); |
| 2119 | } catch (NativeDaemonConnectorException e) { |
| 2120 | rc = StorageResultCode.OperationFailedInternalError; |
| 2121 | } |
| 2122 | return rc; |
| 2123 | } |
| 2124 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2125 | public int finalizeSecureContainer(String id) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2126 | enforcePermission(android.Manifest.permission.ASEC_CREATE); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2127 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2128 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2129 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2130 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2131 | mConnector.execute("asec", "finalize", id); |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 2132 | /* |
| 2133 | * Finalization does a remount, so no need |
| 2134 | * to update mAsecMountSet |
| 2135 | */ |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2136 | } catch (NativeDaemonConnectorException e) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2137 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 2138 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2139 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 2140 | } |
| 2141 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 2142 | public int fixPermissionsSecureContainer(String id, int gid, String filename) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2143 | enforcePermission(android.Manifest.permission.ASEC_CREATE); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 2144 | warnOnNotMounted(); |
| 2145 | |
| 2146 | int rc = StorageResultCode.OperationSucceeded; |
| 2147 | try { |
| 2148 | mConnector.execute("asec", "fixperms", id, gid, filename); |
| 2149 | /* |
| 2150 | * Fix permissions does a remount, so no need to update |
| 2151 | * mAsecMountSet |
| 2152 | */ |
| 2153 | } catch (NativeDaemonConnectorException e) { |
| 2154 | rc = StorageResultCode.OperationFailedInternalError; |
| 2155 | } |
| 2156 | return rc; |
| 2157 | } |
| 2158 | |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 2159 | public int destroySecureContainer(String id, boolean force) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2160 | enforcePermission(android.Manifest.permission.ASEC_DESTROY); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 2161 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2162 | warnOnNotMounted(); |
San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 2163 | |
Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 2164 | /* |
| 2165 | * Force a GC to make sure AssetManagers in other threads of the |
| 2166 | * system_server are cleaned up. We have to do this since AssetManager |
| 2167 | * instances are kept as a WeakReference and it's possible we have files |
| 2168 | * open on the external storage. |
| 2169 | */ |
| 2170 | Runtime.getRuntime().gc(); |
| 2171 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2172 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2173 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2174 | final Command cmd = new Command("asec", "destroy", id); |
| 2175 | if (force) { |
| 2176 | cmd.appendArg("force"); |
| 2177 | } |
| 2178 | mConnector.execute(cmd); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2179 | } catch (NativeDaemonConnectorException e) { |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 2180 | int code = e.getCode(); |
| 2181 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 2182 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 2183 | } else { |
| 2184 | rc = StorageResultCode.OperationFailedInternalError; |
| 2185 | } |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 2186 | } |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 2187 | |
| 2188 | if (rc == StorageResultCode.OperationSucceeded) { |
| 2189 | synchronized (mAsecMountSet) { |
| 2190 | if (mAsecMountSet.contains(id)) { |
| 2191 | mAsecMountSet.remove(id); |
| 2192 | } |
| 2193 | } |
| 2194 | } |
| 2195 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2196 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 2197 | } |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2198 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2199 | public int mountSecureContainer(String id, String key, int ownerUid, boolean readOnly) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2200 | enforcePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 2201 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2202 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2203 | |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 2204 | synchronized (mAsecMountSet) { |
| 2205 | if (mAsecMountSet.contains(id)) { |
| 2206 | return StorageResultCode.OperationFailedStorageMounted; |
| 2207 | } |
| 2208 | } |
| 2209 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2210 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2211 | try { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2212 | mConnector.execute("asec", "mount", id, new SensitiveArg(key), ownerUid, |
| 2213 | readOnly ? "ro" : "rw"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2214 | } catch (NativeDaemonConnectorException e) { |
Kenny Root | f030462 | 2010-03-19 19:20:42 -0700 | [diff] [blame] | 2215 | int code = e.getCode(); |
| 2216 | if (code != VoldResponseCode.OpFailedStorageBusy) { |
| 2217 | rc = StorageResultCode.OperationFailedInternalError; |
| 2218 | } |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 2219 | } |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 2220 | |
| 2221 | if (rc == StorageResultCode.OperationSucceeded) { |
| 2222 | synchronized (mAsecMountSet) { |
| 2223 | mAsecMountSet.add(id); |
| 2224 | } |
| 2225 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2226 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 2227 | } |
| 2228 | |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 2229 | public int unmountSecureContainer(String id, boolean force) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2230 | enforcePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 2231 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2232 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2233 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 2234 | synchronized (mAsecMountSet) { |
| 2235 | if (!mAsecMountSet.contains(id)) { |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 2236 | return StorageResultCode.OperationFailedStorageNotMounted; |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 2237 | } |
| 2238 | } |
| 2239 | |
Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 2240 | /* |
| 2241 | * Force a GC to make sure AssetManagers in other threads of the |
| 2242 | * system_server are cleaned up. We have to do this since AssetManager |
| 2243 | * instances are kept as a WeakReference and it's possible we have files |
| 2244 | * open on the external storage. |
| 2245 | */ |
| 2246 | Runtime.getRuntime().gc(); |
| 2247 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2248 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2249 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2250 | final Command cmd = new Command("asec", "unmount", id); |
| 2251 | if (force) { |
| 2252 | cmd.appendArg("force"); |
| 2253 | } |
| 2254 | mConnector.execute(cmd); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2255 | } catch (NativeDaemonConnectorException e) { |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 2256 | int code = e.getCode(); |
| 2257 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 2258 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 2259 | } else { |
| 2260 | rc = StorageResultCode.OperationFailedInternalError; |
| 2261 | } |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 2262 | } |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 2263 | |
| 2264 | if (rc == StorageResultCode.OperationSucceeded) { |
| 2265 | synchronized (mAsecMountSet) { |
| 2266 | mAsecMountSet.remove(id); |
| 2267 | } |
| 2268 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2269 | return rc; |
San Mehat | 9dba709 | 2010-01-18 06:47:41 -0800 | [diff] [blame] | 2270 | } |
| 2271 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 2272 | public boolean isSecureContainerMounted(String id) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2273 | enforcePermission(android.Manifest.permission.ASEC_ACCESS); |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 2274 | waitForReady(); |
| 2275 | warnOnNotMounted(); |
| 2276 | |
| 2277 | synchronized (mAsecMountSet) { |
| 2278 | return mAsecMountSet.contains(id); |
| 2279 | } |
| 2280 | } |
| 2281 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2282 | public int renameSecureContainer(String oldId, String newId) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2283 | enforcePermission(android.Manifest.permission.ASEC_RENAME); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 2284 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2285 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2286 | |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 2287 | synchronized (mAsecMountSet) { |
San Mehat | 85451ee | 2010-02-24 08:54:18 -0800 | [diff] [blame] | 2288 | /* |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2289 | * Because a mounted container has active internal state which cannot be |
San Mehat | 85451ee | 2010-02-24 08:54:18 -0800 | [diff] [blame] | 2290 | * changed while active, we must ensure both ids are not currently mounted. |
| 2291 | */ |
| 2292 | if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) { |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 2293 | return StorageResultCode.OperationFailedStorageMounted; |
| 2294 | } |
| 2295 | } |
| 2296 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2297 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2298 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2299 | mConnector.execute("asec", "rename", oldId, newId); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2300 | } catch (NativeDaemonConnectorException e) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2301 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 2302 | } |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 2303 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2304 | return rc; |
San Mehat | 45f6104 | 2010-01-23 08:12:43 -0800 | [diff] [blame] | 2305 | } |
| 2306 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 2307 | public String getSecureContainerPath(String id) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2308 | enforcePermission(android.Manifest.permission.ASEC_ACCESS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 2309 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 2310 | warnOnNotMounted(); |
San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 2311 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2312 | final NativeDaemonEvent event; |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 2313 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2314 | event = mConnector.execute("asec", "path", id); |
| 2315 | event.checkCode(VoldResponseCode.AsecPathResult); |
| 2316 | return event.getMessage(); |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 2317 | } catch (NativeDaemonConnectorException e) { |
| 2318 | int code = e.getCode(); |
| 2319 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
Fredrik Helmer | a20c8ef | 2011-02-09 16:16:10 +0100 | [diff] [blame] | 2320 | Slog.i(TAG, String.format("Container '%s' not found", id)); |
| 2321 | return null; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 2322 | } else { |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 2323 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 2324 | } |
| 2325 | } |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 2326 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 2327 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 2328 | public String getSecureContainerFilesystemPath(String id) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2329 | enforcePermission(android.Manifest.permission.ASEC_ACCESS); |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 2330 | waitForReady(); |
| 2331 | warnOnNotMounted(); |
| 2332 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2333 | final NativeDaemonEvent event; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 2334 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2335 | event = mConnector.execute("asec", "fspath", id); |
| 2336 | event.checkCode(VoldResponseCode.AsecPathResult); |
| 2337 | return event.getMessage(); |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 2338 | } catch (NativeDaemonConnectorException e) { |
| 2339 | int code = e.getCode(); |
| 2340 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
| 2341 | Slog.i(TAG, String.format("Container '%s' not found", id)); |
| 2342 | return null; |
| 2343 | } else { |
| 2344 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 2345 | } |
| 2346 | } |
| 2347 | } |
| 2348 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2349 | @Override |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 2350 | public void finishMediaUpdate() { |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 2351 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2352 | throw new SecurityException("no permission to call finishMediaUpdate()"); |
| 2353 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2354 | if (mUnmountSignal != null) { |
| 2355 | mUnmountSignal.countDown(); |
| 2356 | } else { |
| 2357 | Slog.w(TAG, "Odd, nobody asked to unmount?"); |
| 2358 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 2359 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2360 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2361 | private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) { |
| 2362 | if (callerUid == android.os.Process.SYSTEM_UID) { |
| 2363 | return true; |
| 2364 | } |
| 2365 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2366 | if (packageName == null) { |
| 2367 | return false; |
| 2368 | } |
| 2369 | |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 2370 | final int packageUid = mPms.getPackageUid(packageName, |
| 2371 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, UserHandle.getUserId(callerUid)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2372 | |
| 2373 | if (DEBUG_OBB) { |
| 2374 | Slog.d(TAG, "packageName = " + packageName + ", packageUid = " + |
| 2375 | packageUid + ", callerUid = " + callerUid); |
| 2376 | } |
| 2377 | |
| 2378 | return callerUid == packageUid; |
| 2379 | } |
| 2380 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2381 | public String getMountedObbPath(String rawPath) { |
| 2382 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2383 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2384 | waitForReady(); |
| 2385 | warnOnNotMounted(); |
| 2386 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2387 | final ObbState state; |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2388 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2389 | state = mObbPathToStateMap.get(rawPath); |
| 2390 | } |
| 2391 | if (state == null) { |
| 2392 | Slog.w(TAG, "Failed to find OBB mounted at " + rawPath); |
| 2393 | return null; |
| 2394 | } |
| 2395 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2396 | final NativeDaemonEvent event; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2397 | try { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2398 | event = mConnector.execute("obb", "path", state.canonicalPath); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2399 | event.checkCode(VoldResponseCode.AsecPathResult); |
| 2400 | return event.getMessage(); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2401 | } catch (NativeDaemonConnectorException e) { |
| 2402 | int code = e.getCode(); |
| 2403 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2404 | return null; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2405 | } else { |
| 2406 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 2407 | } |
| 2408 | } |
| 2409 | } |
| 2410 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2411 | @Override |
| 2412 | public boolean isObbMounted(String rawPath) { |
| 2413 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2414 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2415 | return mObbPathToStateMap.containsKey(rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2416 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2419 | @Override |
| 2420 | public void mountObb( |
| 2421 | String rawPath, String canonicalPath, String key, IObbActionListener token, int nonce) { |
| 2422 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
| 2423 | Preconditions.checkNotNull(canonicalPath, "canonicalPath cannot be null"); |
| 2424 | Preconditions.checkNotNull(token, "token cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2425 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2426 | final int callingUid = Binder.getCallingUid(); |
| 2427 | final ObbState obbState = new ObbState(rawPath, canonicalPath, callingUid, token, nonce); |
| 2428 | final ObbAction action = new MountObbAction(obbState, key, callingUid); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2429 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
| 2430 | |
| 2431 | if (DEBUG_OBB) |
| 2432 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2435 | @Override |
| 2436 | public void unmountObb(String rawPath, boolean force, IObbActionListener token, int nonce) { |
| 2437 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
| 2438 | |
| 2439 | final ObbState existingState; |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2440 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2441 | existingState = mObbPathToStateMap.get(rawPath); |
Kenny Root | f1121dc | 2010-09-29 07:30:53 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2444 | if (existingState != null) { |
| 2445 | // TODO: separate state object from request data |
| 2446 | final int callingUid = Binder.getCallingUid(); |
| 2447 | final ObbState newState = new ObbState( |
| 2448 | rawPath, existingState.canonicalPath, callingUid, token, nonce); |
| 2449 | final ObbAction action = new UnmountObbAction(newState, force); |
| 2450 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2451 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2452 | if (DEBUG_OBB) |
| 2453 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
| 2454 | } else { |
| 2455 | Slog.w(TAG, "Unknown OBB mount at " + rawPath); |
| 2456 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2459 | @Override |
| 2460 | public int getEncryptionState() { |
| 2461 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2462 | "no permission to access the crypt keeper"); |
| 2463 | |
| 2464 | waitForReady(); |
| 2465 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2466 | final NativeDaemonEvent event; |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2467 | try { |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 2468 | event = mCryptConnector.execute("cryptfs", "cryptocomplete"); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2469 | return Integer.parseInt(event.getMessage()); |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2470 | } catch (NumberFormatException e) { |
| 2471 | // Bad result - unexpected. |
| 2472 | Slog.w(TAG, "Unable to parse result from cryptfs cryptocomplete"); |
| 2473 | return ENCRYPTION_STATE_ERROR_UNKNOWN; |
| 2474 | } catch (NativeDaemonConnectorException e) { |
| 2475 | // Something bad happened. |
| 2476 | Slog.w(TAG, "Error in communicating with cryptfs in validating"); |
| 2477 | return ENCRYPTION_STATE_ERROR_UNKNOWN; |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | @Override |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2482 | public int decryptStorage(String password) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2483 | if (TextUtils.isEmpty(password)) { |
| 2484 | throw new IllegalArgumentException("password cannot be empty"); |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2485 | } |
| 2486 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2487 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2488 | "no permission to access the crypt keeper"); |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2489 | |
| 2490 | waitForReady(); |
| 2491 | |
| 2492 | if (DEBUG_EVENTS) { |
| 2493 | Slog.i(TAG, "decrypting storage..."); |
| 2494 | } |
| 2495 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2496 | final NativeDaemonEvent event; |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2497 | try { |
Paul Lawrence | 0548761 | 2015-06-09 13:35:38 -0700 | [diff] [blame] | 2498 | event = mCryptConnector.execute("cryptfs", "checkpw", new SensitiveArg(password)); |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2499 | |
Fredrik Roubert | da6aedf | 2011-12-20 17:34:43 +0100 | [diff] [blame] | 2500 | final int code = Integer.parseInt(event.getMessage()); |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2501 | if (code == 0) { |
| 2502 | // Decrypt was successful. Post a delayed message before restarting in order |
| 2503 | // to let the UI to clear itself |
| 2504 | mHandler.postDelayed(new Runnable() { |
| 2505 | public void run() { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 2506 | try { |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 2507 | mCryptConnector.execute("cryptfs", "restart"); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 2508 | } catch (NativeDaemonConnectorException e) { |
| 2509 | Slog.e(TAG, "problem executing in background", e); |
| 2510 | } |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2511 | } |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2512 | }, 1000); // 1 second |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | return code; |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2516 | } catch (NativeDaemonConnectorException e) { |
| 2517 | // Decryption failed |
| 2518 | return e.getCode(); |
| 2519 | } |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2520 | } |
| 2521 | |
Paul Lawrence | 46791e7 | 2014-04-03 09:10:26 -0700 | [diff] [blame] | 2522 | public int encryptStorage(int type, String password) { |
| 2523 | if (TextUtils.isEmpty(password) && type != StorageManager.CRYPT_TYPE_DEFAULT) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2524 | throw new IllegalArgumentException("password cannot be empty"); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2525 | } |
| 2526 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2527 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2528 | "no permission to access the crypt keeper"); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2529 | |
| 2530 | waitForReady(); |
| 2531 | |
| 2532 | if (DEBUG_EVENTS) { |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2533 | Slog.i(TAG, "encrypting storage..."); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | try { |
Paul Lawrence | 5096d9e | 2015-09-09 13:05:45 -0700 | [diff] [blame] | 2537 | if (type == StorageManager.CRYPT_TYPE_DEFAULT) { |
| 2538 | mCryptConnector.execute("cryptfs", "enablecrypto", "inplace", |
| 2539 | CRYPTO_TYPES[type]); |
| 2540 | } else { |
| 2541 | mCryptConnector.execute("cryptfs", "enablecrypto", "inplace", |
| 2542 | CRYPTO_TYPES[type], new SensitiveArg(password)); |
| 2543 | } |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2544 | } catch (NativeDaemonConnectorException e) { |
| 2545 | // Encryption failed |
| 2546 | return e.getCode(); |
| 2547 | } |
| 2548 | |
| 2549 | return 0; |
| 2550 | } |
| 2551 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2552 | /** Set the password for encrypting the master key. |
| 2553 | * @param type One of the CRYPTO_TYPE_XXX consts defined in StorageManager. |
| 2554 | * @param password The password to set. |
| 2555 | */ |
| 2556 | public int changeEncryptionPassword(int type, String password) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2557 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2558 | "no permission to access the crypt keeper"); |
| 2559 | |
| 2560 | waitForReady(); |
| 2561 | |
| 2562 | if (DEBUG_EVENTS) { |
| 2563 | Slog.i(TAG, "changing encryption password..."); |
| 2564 | } |
| 2565 | |
| 2566 | try { |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 2567 | NativeDaemonEvent event = mCryptConnector.execute("cryptfs", "changepw", CRYPTO_TYPES[type], |
Paul Lawrence | 0548761 | 2015-06-09 13:35:38 -0700 | [diff] [blame] | 2568 | new SensitiveArg(password)); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2569 | return Integer.parseInt(event.getMessage()); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2570 | } catch (NativeDaemonConnectorException e) { |
| 2571 | // Encryption failed |
| 2572 | return e.getCode(); |
| 2573 | } |
| 2574 | } |
| 2575 | |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2576 | /** |
| 2577 | * Validate a user-supplied password string with cryptfs |
| 2578 | */ |
| 2579 | @Override |
| 2580 | public int verifyEncryptionPassword(String password) throws RemoteException { |
| 2581 | // Only the system process is permitted to validate passwords |
| 2582 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 2583 | throw new SecurityException("no permission to access the crypt keeper"); |
| 2584 | } |
| 2585 | |
| 2586 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2587 | "no permission to access the crypt keeper"); |
| 2588 | |
| 2589 | if (TextUtils.isEmpty(password)) { |
| 2590 | throw new IllegalArgumentException("password cannot be empty"); |
| 2591 | } |
| 2592 | |
| 2593 | waitForReady(); |
| 2594 | |
| 2595 | if (DEBUG_EVENTS) { |
| 2596 | Slog.i(TAG, "validating encryption password..."); |
| 2597 | } |
| 2598 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2599 | final NativeDaemonEvent event; |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2600 | try { |
Paul Lawrence | 0548761 | 2015-06-09 13:35:38 -0700 | [diff] [blame] | 2601 | event = mCryptConnector.execute("cryptfs", "verifypw", new SensitiveArg(password)); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2602 | Slog.i(TAG, "cryptfs verifypw => " + event.getMessage()); |
| 2603 | return Integer.parseInt(event.getMessage()); |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2604 | } catch (NativeDaemonConnectorException e) { |
| 2605 | // Encryption failed |
| 2606 | return e.getCode(); |
| 2607 | } |
| 2608 | } |
| 2609 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2610 | /** |
| 2611 | * Get the type of encryption used to encrypt the master key. |
| 2612 | * @return The type, one of the CRYPT_TYPE_XXX consts from StorageManager. |
| 2613 | */ |
| 2614 | @Override |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 2615 | public int getPasswordType() { |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2616 | |
| 2617 | waitForReady(); |
| 2618 | |
| 2619 | final NativeDaemonEvent event; |
| 2620 | try { |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 2621 | event = mCryptConnector.execute("cryptfs", "getpwtype"); |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 2622 | for (int i = 0; i < CRYPTO_TYPES.length; ++i) { |
| 2623 | if (CRYPTO_TYPES[i].equals(event.getMessage())) |
| 2624 | return i; |
| 2625 | } |
| 2626 | |
| 2627 | throw new IllegalStateException("unexpected return from cryptfs"); |
| 2628 | } catch (NativeDaemonConnectorException e) { |
| 2629 | throw e.rethrowAsParcelableException(); |
| 2630 | } |
| 2631 | } |
| 2632 | |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2633 | /** |
| 2634 | * Set a field in the crypto header. |
| 2635 | * @param field field to set |
| 2636 | * @param contents contents to set in field |
| 2637 | */ |
| 2638 | @Override |
| 2639 | public void setField(String field, String contents) throws RemoteException { |
| 2640 | |
| 2641 | waitForReady(); |
| 2642 | |
| 2643 | final NativeDaemonEvent event; |
| 2644 | try { |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 2645 | event = mCryptConnector.execute("cryptfs", "setfield", field, contents); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2646 | } catch (NativeDaemonConnectorException e) { |
| 2647 | throw e.rethrowAsParcelableException(); |
| 2648 | } |
| 2649 | } |
| 2650 | |
| 2651 | /** |
| 2652 | * Gets a field from the crypto header. |
| 2653 | * @param field field to get |
| 2654 | * @return contents of field |
| 2655 | */ |
| 2656 | @Override |
| 2657 | public String getField(String field) throws RemoteException { |
| 2658 | |
| 2659 | waitForReady(); |
| 2660 | |
| 2661 | final NativeDaemonEvent event; |
| 2662 | try { |
| 2663 | final String[] contents = NativeDaemonEvent.filterMessageList( |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 2664 | mCryptConnector.executeForList("cryptfs", "getfield", field), |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 2665 | VoldResponseCode.CryptfsGetfieldResult); |
| 2666 | String result = new String(); |
| 2667 | for (String content : contents) { |
| 2668 | result += content; |
| 2669 | } |
| 2670 | return result; |
| 2671 | } catch (NativeDaemonConnectorException e) { |
| 2672 | throw e.rethrowAsParcelableException(); |
| 2673 | } |
| 2674 | } |
| 2675 | |
Paul Lawrence | 3806d9c | 2015-10-29 10:30:46 -0700 | [diff] [blame] | 2676 | /** |
| 2677 | * Is userdata convertible to file based encryption? |
| 2678 | * @return non zero for convertible |
| 2679 | */ |
| 2680 | @Override |
| 2681 | public boolean isConvertibleToFBE() throws RemoteException { |
| 2682 | |
| 2683 | waitForReady(); |
| 2684 | |
| 2685 | final NativeDaemonEvent event; |
| 2686 | try { |
| 2687 | event = mCryptConnector.execute("cryptfs", "isConvertibleToFBE"); |
| 2688 | return Integer.parseInt(event.getMessage()) != 0; |
| 2689 | } catch (NativeDaemonConnectorException e) { |
| 2690 | throw e.rethrowAsParcelableException(); |
| 2691 | } |
| 2692 | } |
| 2693 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 2694 | @Override |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2695 | public String getPassword() throws RemoteException { |
Rubin Xu | cd7a014 | 2015-04-17 23:45:27 +0100 | [diff] [blame] | 2696 | mContext.enforceCallingOrSelfPermission(Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE, |
| 2697 | "only keyguard can retrieve password"); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2698 | if (!isReady()) { |
| 2699 | return new String(); |
| 2700 | } |
| 2701 | |
| 2702 | final NativeDaemonEvent event; |
| 2703 | try { |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 2704 | event = mCryptConnector.execute("cryptfs", "getpw"); |
Paul Lawrence | 24063b5 | 2015-01-06 13:11:23 -0800 | [diff] [blame] | 2705 | if ("-1".equals(event.getMessage())) { |
| 2706 | // -1 equals no password |
| 2707 | return null; |
| 2708 | } |
Paul Lawrence | 0548761 | 2015-06-09 13:35:38 -0700 | [diff] [blame] | 2709 | return event.getMessage(); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2710 | } catch (NativeDaemonConnectorException e) { |
| 2711 | throw e.rethrowAsParcelableException(); |
Paul Lawrence | 24063b5 | 2015-01-06 13:11:23 -0800 | [diff] [blame] | 2712 | } catch (IllegalArgumentException e) { |
| 2713 | Slog.e(TAG, "Invalid response to getPassword"); |
| 2714 | return null; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | @Override |
| 2719 | public void clearPassword() throws RemoteException { |
| 2720 | if (!isReady()) { |
| 2721 | return; |
| 2722 | } |
| 2723 | |
| 2724 | final NativeDaemonEvent event; |
| 2725 | try { |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 2726 | event = mCryptConnector.execute("cryptfs", "clearpw"); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2727 | } catch (NativeDaemonConnectorException e) { |
| 2728 | throw e.rethrowAsParcelableException(); |
| 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | @Override |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 2733 | public void createUserKey(int userId, int serialNumber, boolean ephemeral) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2734 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2735 | waitForReady(); |
| 2736 | |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2737 | try { |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 2738 | mCryptConnector.execute("cryptfs", "create_user_key", userId, serialNumber, |
| 2739 | ephemeral ? 1 : 0); |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2740 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2741 | throw e.rethrowAsParcelableException(); |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2742 | } |
| 2743 | } |
| 2744 | |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 2745 | @Override |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2746 | public void destroyUserKey(int userId) { |
| 2747 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 2748 | waitForReady(); |
| 2749 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2750 | try { |
| 2751 | mCryptConnector.execute("cryptfs", "destroy_user_key", userId); |
| 2752 | } catch (NativeDaemonConnectorException e) { |
| 2753 | throw e.rethrowAsParcelableException(); |
| 2754 | } |
| 2755 | } |
| 2756 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2757 | private SensitiveArg encodeBytes(byte[] bytes) { |
| 2758 | if (ArrayUtils.isEmpty(bytes)) { |
| 2759 | return new SensitiveArg("!"); |
| 2760 | } else { |
| 2761 | return new SensitiveArg(HexDump.toHexString(bytes)); |
| 2762 | } |
| 2763 | } |
| 2764 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2765 | @Override |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2766 | public void changeUserKey(int userId, int serialNumber, |
| 2767 | byte[] token, byte[] oldSecret, byte[] newSecret) { |
| 2768 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
| 2769 | waitForReady(); |
| 2770 | |
| 2771 | try { |
| 2772 | mCryptConnector.execute("cryptfs", "change_user_key", userId, serialNumber, |
| 2773 | encodeBytes(token), encodeBytes(oldSecret), encodeBytes(newSecret)); |
| 2774 | } catch (NativeDaemonConnectorException e) { |
| 2775 | throw e.rethrowAsParcelableException(); |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | @Override |
| 2780 | public void unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2781 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
| 2782 | waitForReady(); |
| 2783 | |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 2784 | // When a user has secure lock screen, require a challenge token to |
| 2785 | // actually unlock. This check is mostly in place for emulation mode. |
| 2786 | if (mLockPatternUtils.isSecure(userId) && ArrayUtils.isEmpty(token)) { |
| 2787 | throw new IllegalStateException("Token required to unlock secure user " + userId); |
| 2788 | } |
| 2789 | |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 2790 | try { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2791 | mCryptConnector.execute("cryptfs", "unlock_user_key", userId, serialNumber, |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2792 | encodeBytes(token), encodeBytes(secret)); |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 2793 | } catch (NativeDaemonConnectorException e) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2794 | throw e.rethrowAsParcelableException(); |
| 2795 | } |
| 2796 | |
| 2797 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2798 | mLocalUnlockedUsers = ArrayUtils.appendInt(mLocalUnlockedUsers, userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | @Override |
| 2803 | public void lockUserKey(int userId) { |
| 2804 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
| 2805 | waitForReady(); |
| 2806 | |
| 2807 | try { |
| 2808 | mCryptConnector.execute("cryptfs", "lock_user_key", userId); |
| 2809 | } catch (NativeDaemonConnectorException e) { |
| 2810 | throw e.rethrowAsParcelableException(); |
| 2811 | } |
| 2812 | |
| 2813 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2814 | mLocalUnlockedUsers = ArrayUtils.removeInt(mLocalUnlockedUsers, userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | @Override |
| 2819 | public boolean isUserKeyUnlocked(int userId) { |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 2820 | if (StorageManager.isFileEncryptedNativeOrEmulated()) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2821 | synchronized (mLock) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2822 | return ArrayUtils.contains(mLocalUnlockedUsers, userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2823 | } |
| 2824 | } else { |
| 2825 | return true; |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | @Override |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 2830 | public void prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2831 | enforcePermission(android.Manifest.permission.STORAGE_INTERNAL); |
| 2832 | waitForReady(); |
| 2833 | |
| 2834 | try { |
| 2835 | mCryptConnector.execute("cryptfs", "prepare_user_storage", escapeNull(volumeUuid), |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 2836 | userId, serialNumber, flags); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2837 | } catch (NativeDaemonConnectorException e) { |
| 2838 | throw e.rethrowAsParcelableException(); |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 2839 | } |
| 2840 | } |
| 2841 | |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 2842 | @Override |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 2843 | public ParcelFileDescriptor mountAppFuse(final String name) throws RemoteException { |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 2844 | try { |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 2845 | final int uid = Binder.getCallingUid(); |
Daichi Hirono | fd7d57e | 2016-01-29 14:30:58 +0900 | [diff] [blame] | 2846 | final int pid = Binder.getCallingPid(); |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 2847 | final NativeDaemonEvent event = |
Daichi Hirono | fd7d57e | 2016-01-29 14:30:58 +0900 | [diff] [blame] | 2848 | mConnector.execute("appfuse", "mount", uid, pid, name); |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 2849 | if (event.getFileDescriptors() == null) { |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 2850 | throw new RemoteException("AppFuse FD from vold is null."); |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 2851 | } |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 2852 | return ParcelFileDescriptor.fromFd( |
| 2853 | event.getFileDescriptors()[0], |
| 2854 | mHandler, |
| 2855 | new ParcelFileDescriptor.OnCloseListener() { |
| 2856 | @Override |
| 2857 | public void onClose(IOException e) { |
| 2858 | try { |
| 2859 | final NativeDaemonEvent event = mConnector.execute( |
Daichi Hirono | fd7d57e | 2016-01-29 14:30:58 +0900 | [diff] [blame] | 2860 | "appfuse", "unmount", uid, pid, name); |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 2861 | } catch (NativeDaemonConnectorException unmountException) { |
| 2862 | Log.e(TAG, "Failed to unmount appfuse."); |
| 2863 | } |
| 2864 | } |
| 2865 | }); |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 2866 | } catch (NativeDaemonConnectorException e) { |
| 2867 | throw e.rethrowAsParcelableException(); |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 2868 | } catch (IOException e) { |
| 2869 | throw new RemoteException(e.getMessage()); |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 2870 | } |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | @Override |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2874 | public int mkdirs(String callingPkg, String appPath) { |
| 2875 | final int userId = UserHandle.getUserId(Binder.getCallingUid()); |
| 2876 | final UserEnvironment userEnv = new UserEnvironment(userId); |
| 2877 | |
| 2878 | // Validate that reported package name belongs to caller |
| 2879 | final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService( |
| 2880 | Context.APP_OPS_SERVICE); |
| 2881 | appOps.checkPackage(Binder.getCallingUid(), callingPkg); |
| 2882 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2883 | File appFile = null; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2884 | try { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2885 | appFile = new File(appPath).getCanonicalFile(); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2886 | } catch (IOException e) { |
| 2887 | Slog.e(TAG, "Failed to resolve " + appPath + ": " + e); |
| 2888 | return -1; |
| 2889 | } |
| 2890 | |
| 2891 | // Try translating the app path into a vold path, but require that it |
| 2892 | // belong to the calling package. |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2893 | if (FileUtils.contains(userEnv.buildExternalStorageAppDataDirs(callingPkg), appFile) || |
| 2894 | FileUtils.contains(userEnv.buildExternalStorageAppObbDirs(callingPkg), appFile) || |
| 2895 | FileUtils.contains(userEnv.buildExternalStorageAppMediaDirs(callingPkg), appFile)) { |
| 2896 | appPath = appFile.getAbsolutePath(); |
| 2897 | if (!appPath.endsWith("/")) { |
| 2898 | appPath = appPath + "/"; |
| 2899 | } |
| 2900 | |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2901 | try { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2902 | mConnector.execute("volume", "mkdirs", appPath); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2903 | return 0; |
| 2904 | } catch (NativeDaemonConnectorException e) { |
| 2905 | return e.getCode(); |
| 2906 | } |
| 2907 | } |
| 2908 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2909 | throw new SecurityException("Invalid mkdirs path: " + appFile); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | @Override |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 2913 | public StorageVolume[] getVolumeList(int uid, String packageName, int flags) { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2914 | final int userId = UserHandle.getUserId(uid); |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 2915 | final boolean forWrite = (flags & StorageManager.FLAG_FOR_WRITE) != 0; |
| 2916 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2917 | boolean reportUnmounted = false; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2918 | boolean foundPrimary = false; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2919 | |
Svetoslav | 38c3dbb | 2015-07-14 11:27:06 -0700 | [diff] [blame] | 2920 | final long identity = Binder.clearCallingIdentity(); |
| 2921 | try { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2922 | if (!mMountServiceInternal.hasExternalStorage(uid, packageName)) { |
| 2923 | reportUnmounted = true; |
| 2924 | } |
| 2925 | if (!isUserKeyUnlocked(userId)) { |
| 2926 | reportUnmounted = true; |
| 2927 | } |
Svetoslav | 38c3dbb | 2015-07-14 11:27:06 -0700 | [diff] [blame] | 2928 | } finally { |
| 2929 | Binder.restoreCallingIdentity(identity); |
| 2930 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 2931 | |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 2932 | final ArrayList<StorageVolume> res = new ArrayList<>(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2933 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2934 | for (int i = 0; i < mVolumes.size(); i++) { |
| 2935 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 2936 | if (forWrite ? vol.isVisibleForWrite(userId) : vol.isVisibleForRead(userId)) { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 2937 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, |
| 2938 | reportUnmounted); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2939 | if (vol.isPrimary()) { |
| 2940 | res.add(0, userVol); |
| 2941 | foundPrimary = true; |
| 2942 | } else { |
| 2943 | res.add(userVol); |
| 2944 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 2945 | } |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 2946 | } |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 2947 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2948 | |
| 2949 | if (!foundPrimary) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2950 | Log.w(TAG, "No primary storage defined yet; hacking together a stub"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2951 | |
| 2952 | final boolean primaryPhysical = SystemProperties.getBoolean( |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2953 | StorageManager.PROP_PRIMARY_PHYSICAL, false); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2954 | |
| 2955 | final String id = "stub_primary"; |
| 2956 | final File path = Environment.getLegacyExternalStorageDirectory(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2957 | final String description = mContext.getString(android.R.string.unknownName); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2958 | final boolean primary = true; |
| 2959 | final boolean removable = primaryPhysical; |
| 2960 | final boolean emulated = !primaryPhysical; |
| 2961 | final long mtpReserveSize = 0L; |
| 2962 | final boolean allowMassStorage = false; |
| 2963 | final long maxFileSize = 0L; |
| 2964 | final UserHandle owner = new UserHandle(userId); |
| 2965 | final String uuid = null; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2966 | final String state = Environment.MEDIA_REMOVED; |
| 2967 | |
Jeff Sharkey | 5af1835d | 2015-07-07 17:26:59 -0700 | [diff] [blame] | 2968 | res.add(0, new StorageVolume(id, StorageVolume.STORAGE_ID_INVALID, path, |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2969 | description, primary, removable, emulated, mtpReserveSize, |
| 2970 | allowMassStorage, maxFileSize, owner, uuid, state)); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | return res.toArray(new StorageVolume[res.size()]); |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 2974 | } |
| 2975 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2976 | @Override |
| 2977 | public DiskInfo[] getDisks() { |
| 2978 | synchronized (mLock) { |
| 2979 | final DiskInfo[] res = new DiskInfo[mDisks.size()]; |
| 2980 | for (int i = 0; i < mDisks.size(); i++) { |
| 2981 | res[i] = mDisks.valueAt(i); |
| 2982 | } |
| 2983 | return res; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | @Override |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2988 | public VolumeInfo[] getVolumes(int flags) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2989 | synchronized (mLock) { |
| 2990 | final VolumeInfo[] res = new VolumeInfo[mVolumes.size()]; |
| 2991 | for (int i = 0; i < mVolumes.size(); i++) { |
| 2992 | res[i] = mVolumes.valueAt(i); |
| 2993 | } |
| 2994 | return res; |
| 2995 | } |
| 2996 | } |
| 2997 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 2998 | @Override |
| 2999 | public VolumeRecord[] getVolumeRecords(int flags) { |
| 3000 | synchronized (mLock) { |
| 3001 | final VolumeRecord[] res = new VolumeRecord[mRecords.size()]; |
| 3002 | for (int i = 0; i < mRecords.size(); i++) { |
| 3003 | res[i] = mRecords.valueAt(i); |
| 3004 | } |
| 3005 | return res; |
| 3006 | } |
| 3007 | } |
| 3008 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3009 | private void addObbStateLocked(ObbState obbState) throws RemoteException { |
| 3010 | final IBinder binder = obbState.getBinder(); |
| 3011 | List<ObbState> obbStates = mObbMounts.get(binder); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 3012 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3013 | if (obbStates == null) { |
| 3014 | obbStates = new ArrayList<ObbState>(); |
| 3015 | mObbMounts.put(binder, obbStates); |
| 3016 | } else { |
| 3017 | for (final ObbState o : obbStates) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3018 | if (o.rawPath.equals(obbState.rawPath)) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3019 | throw new IllegalStateException("Attempt to add ObbState twice. " |
| 3020 | + "This indicates an error in the MountService logic."); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 3021 | } |
| 3022 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3023 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3024 | |
| 3025 | obbStates.add(obbState); |
| 3026 | try { |
| 3027 | obbState.link(); |
| 3028 | } catch (RemoteException e) { |
| 3029 | /* |
| 3030 | * The binder died before we could link it, so clean up our state |
| 3031 | * and return failure. |
| 3032 | */ |
| 3033 | obbStates.remove(obbState); |
| 3034 | if (obbStates.isEmpty()) { |
| 3035 | mObbMounts.remove(binder); |
| 3036 | } |
| 3037 | |
| 3038 | // Rethrow the error so mountObb can get it |
| 3039 | throw e; |
| 3040 | } |
| 3041 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3042 | mObbPathToStateMap.put(obbState.rawPath, obbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3043 | } |
| 3044 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3045 | private void removeObbStateLocked(ObbState obbState) { |
| 3046 | final IBinder binder = obbState.getBinder(); |
| 3047 | final List<ObbState> obbStates = mObbMounts.get(binder); |
| 3048 | if (obbStates != null) { |
| 3049 | if (obbStates.remove(obbState)) { |
| 3050 | obbState.unlink(); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 3051 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3052 | if (obbStates.isEmpty()) { |
| 3053 | mObbMounts.remove(binder); |
| 3054 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3055 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3056 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3057 | mObbPathToStateMap.remove(obbState.rawPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3058 | } |
| 3059 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3060 | private class ObbActionHandler extends Handler { |
| 3061 | private boolean mBound = false; |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 3062 | private final List<ObbAction> mActions = new LinkedList<ObbAction>(); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3063 | |
| 3064 | ObbActionHandler(Looper l) { |
| 3065 | super(l); |
| 3066 | } |
| 3067 | |
| 3068 | @Override |
| 3069 | public void handleMessage(Message msg) { |
| 3070 | switch (msg.what) { |
| 3071 | case OBB_RUN_ACTION: { |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 3072 | final ObbAction action = (ObbAction) msg.obj; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3073 | |
| 3074 | if (DEBUG_OBB) |
| 3075 | Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString()); |
| 3076 | |
| 3077 | // If a bind was already initiated we don't really |
| 3078 | // need to do anything. The pending install |
| 3079 | // will be processed later on. |
| 3080 | if (!mBound) { |
| 3081 | // If this is the only one pending we might |
| 3082 | // have to bind to the service again. |
| 3083 | if (!connectToService()) { |
| 3084 | Slog.e(TAG, "Failed to bind to media container service"); |
| 3085 | action.handleError(); |
| 3086 | return; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3087 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3088 | } |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 3089 | |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 3090 | mActions.add(action); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3091 | break; |
| 3092 | } |
| 3093 | case OBB_MCS_BOUND: { |
| 3094 | if (DEBUG_OBB) |
| 3095 | Slog.i(TAG, "OBB_MCS_BOUND"); |
| 3096 | if (msg.obj != null) { |
| 3097 | mContainerService = (IMediaContainerService) msg.obj; |
| 3098 | } |
| 3099 | if (mContainerService == null) { |
| 3100 | // Something seriously wrong. Bail out |
| 3101 | Slog.e(TAG, "Cannot bind to media container service"); |
| 3102 | for (ObbAction action : mActions) { |
| 3103 | // Indicate service bind error |
| 3104 | action.handleError(); |
| 3105 | } |
| 3106 | mActions.clear(); |
| 3107 | } else if (mActions.size() > 0) { |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 3108 | final ObbAction action = mActions.get(0); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3109 | if (action != null) { |
| 3110 | action.execute(this); |
| 3111 | } |
| 3112 | } else { |
| 3113 | // Should never happen ideally. |
| 3114 | Slog.w(TAG, "Empty queue"); |
| 3115 | } |
| 3116 | break; |
| 3117 | } |
| 3118 | case OBB_MCS_RECONNECT: { |
| 3119 | if (DEBUG_OBB) |
| 3120 | Slog.i(TAG, "OBB_MCS_RECONNECT"); |
| 3121 | if (mActions.size() > 0) { |
| 3122 | if (mBound) { |
| 3123 | disconnectService(); |
| 3124 | } |
| 3125 | if (!connectToService()) { |
| 3126 | Slog.e(TAG, "Failed to bind to media container service"); |
| 3127 | for (ObbAction action : mActions) { |
| 3128 | // Indicate service bind error |
| 3129 | action.handleError(); |
| 3130 | } |
| 3131 | mActions.clear(); |
| 3132 | } |
| 3133 | } |
| 3134 | break; |
| 3135 | } |
| 3136 | case OBB_MCS_UNBIND: { |
| 3137 | if (DEBUG_OBB) |
| 3138 | Slog.i(TAG, "OBB_MCS_UNBIND"); |
| 3139 | |
| 3140 | // Delete pending install |
| 3141 | if (mActions.size() > 0) { |
| 3142 | mActions.remove(0); |
| 3143 | } |
| 3144 | if (mActions.size() == 0) { |
| 3145 | if (mBound) { |
| 3146 | disconnectService(); |
| 3147 | } |
| 3148 | } else { |
| 3149 | // There are more pending requests in queue. |
| 3150 | // Just post MCS_BOUND message to trigger processing |
| 3151 | // of next pending install. |
| 3152 | mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND); |
| 3153 | } |
| 3154 | break; |
| 3155 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3156 | case OBB_FLUSH_MOUNT_STATE: { |
| 3157 | final String path = (String) msg.obj; |
| 3158 | |
| 3159 | if (DEBUG_OBB) |
| 3160 | Slog.i(TAG, "Flushing all OBB state for path " + path); |
| 3161 | |
| 3162 | synchronized (mObbMounts) { |
| 3163 | final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>(); |
| 3164 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3165 | final Iterator<ObbState> i = mObbPathToStateMap.values().iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3166 | while (i.hasNext()) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3167 | final ObbState state = i.next(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3168 | |
| 3169 | /* |
| 3170 | * If this entry's source file is in the volume path |
| 3171 | * that got unmounted, remove it because it's no |
| 3172 | * longer valid. |
| 3173 | */ |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3174 | if (state.canonicalPath.startsWith(path)) { |
| 3175 | obbStatesToRemove.add(state); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | for (final ObbState obbState : obbStatesToRemove) { |
| 3180 | if (DEBUG_OBB) |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3181 | Slog.i(TAG, "Removing state for " + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3182 | |
| 3183 | removeObbStateLocked(obbState); |
| 3184 | |
| 3185 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3186 | obbState.token.onObbResult(obbState.rawPath, obbState.nonce, |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3187 | OnObbStateChangeListener.UNMOUNTED); |
| 3188 | } catch (RemoteException e) { |
| 3189 | Slog.i(TAG, "Couldn't send unmount notification for OBB: " |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3190 | + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3191 | } |
| 3192 | } |
| 3193 | } |
| 3194 | break; |
| 3195 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | private boolean connectToService() { |
| 3200 | if (DEBUG_OBB) |
| 3201 | Slog.i(TAG, "Trying to bind to DefaultContainerService"); |
| 3202 | |
| 3203 | Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 3204 | if (mContext.bindServiceAsUser(service, mDefContainerConn, Context.BIND_AUTO_CREATE, |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 3205 | UserHandle.SYSTEM)) { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3206 | mBound = true; |
| 3207 | return true; |
| 3208 | } |
| 3209 | return false; |
| 3210 | } |
| 3211 | |
| 3212 | private void disconnectService() { |
| 3213 | mContainerService = null; |
| 3214 | mBound = false; |
| 3215 | mContext.unbindService(mDefContainerConn); |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | abstract class ObbAction { |
| 3220 | private static final int MAX_RETRIES = 3; |
| 3221 | private int mRetries; |
| 3222 | |
| 3223 | ObbState mObbState; |
| 3224 | |
| 3225 | ObbAction(ObbState obbState) { |
| 3226 | mObbState = obbState; |
| 3227 | } |
| 3228 | |
| 3229 | public void execute(ObbActionHandler handler) { |
| 3230 | try { |
| 3231 | if (DEBUG_OBB) |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 3232 | Slog.i(TAG, "Starting to execute action: " + toString()); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3233 | mRetries++; |
| 3234 | if (mRetries > MAX_RETRIES) { |
| 3235 | Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up"); |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 3236 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3237 | handleError(); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3238 | } else { |
| 3239 | handleExecute(); |
| 3240 | if (DEBUG_OBB) |
| 3241 | Slog.i(TAG, "Posting install MCS_UNBIND"); |
| 3242 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
| 3243 | } |
| 3244 | } catch (RemoteException e) { |
| 3245 | if (DEBUG_OBB) |
| 3246 | Slog.i(TAG, "Posting install MCS_RECONNECT"); |
| 3247 | mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT); |
| 3248 | } catch (Exception e) { |
| 3249 | if (DEBUG_OBB) |
| 3250 | Slog.d(TAG, "Error handling OBB action", e); |
| 3251 | handleError(); |
Kenny Root | 17eb6fb | 2010-10-06 15:02:52 -0700 | [diff] [blame] | 3252 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3253 | } |
| 3254 | } |
| 3255 | |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 3256 | abstract void handleExecute() throws RemoteException, IOException; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3257 | abstract void handleError(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3258 | |
| 3259 | protected ObbInfo getObbInfo() throws IOException { |
| 3260 | ObbInfo obbInfo; |
| 3261 | try { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 3262 | obbInfo = mContainerService.getObbInfo(mObbState.canonicalPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3263 | } catch (RemoteException e) { |
| 3264 | Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for " |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 3265 | + mObbState.canonicalPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3266 | obbInfo = null; |
| 3267 | } |
| 3268 | if (obbInfo == null) { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 3269 | throw new IOException("Couldn't read OBB file: " + mObbState.canonicalPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3270 | } |
| 3271 | return obbInfo; |
| 3272 | } |
| 3273 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3274 | protected void sendNewStatusOrIgnore(int status) { |
| 3275 | if (mObbState == null || mObbState.token == null) { |
| 3276 | return; |
| 3277 | } |
| 3278 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3279 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3280 | mObbState.token.onObbResult(mObbState.rawPath, mObbState.nonce, status); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3281 | } catch (RemoteException e) { |
| 3282 | Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged"); |
| 3283 | } |
| 3284 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | class MountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 3288 | private final String mKey; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3289 | private final int mCallingUid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3290 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3291 | MountObbAction(ObbState obbState, String key, int callingUid) { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3292 | super(obbState); |
| 3293 | mKey = key; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3294 | mCallingUid = callingUid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3295 | } |
| 3296 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3297 | @Override |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 3298 | public void handleExecute() throws IOException, RemoteException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3299 | waitForReady(); |
| 3300 | warnOnNotMounted(); |
| 3301 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3302 | final ObbInfo obbInfo = getObbInfo(); |
| 3303 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3304 | if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mCallingUid)) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3305 | Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename |
| 3306 | + " which is owned by " + obbInfo.packageName); |
| 3307 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED); |
| 3308 | return; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3309 | } |
| 3310 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3311 | final boolean isMounted; |
| 3312 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3313 | isMounted = mObbPathToStateMap.containsKey(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3314 | } |
| 3315 | if (isMounted) { |
| 3316 | Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename); |
| 3317 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED); |
| 3318 | return; |
| 3319 | } |
| 3320 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3321 | final String hashedKey; |
| 3322 | if (mKey == null) { |
| 3323 | hashedKey = "none"; |
| 3324 | } else { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3325 | try { |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 3326 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 3327 | |
| 3328 | KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt, |
| 3329 | PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE); |
| 3330 | SecretKey key = factory.generateSecret(ks); |
| 3331 | BigInteger bi = new BigInteger(key.getEncoded()); |
| 3332 | hashedKey = bi.toString(16); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3333 | } catch (NoSuchAlgorithmException e) { |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 3334 | Slog.e(TAG, "Could not load PBKDF2 algorithm", e); |
| 3335 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
| 3336 | return; |
| 3337 | } catch (InvalidKeySpecException e) { |
| 3338 | Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e); |
| 3339 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3340 | return; |
| 3341 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3342 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3343 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3344 | int rc = StorageResultCode.OperationSucceeded; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3345 | try { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 3346 | mConnector.execute("obb", "mount", mObbState.canonicalPath, new SensitiveArg(hashedKey), |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 3347 | mObbState.ownerGid); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3348 | } catch (NativeDaemonConnectorException e) { |
| 3349 | int code = e.getCode(); |
| 3350 | if (code != VoldResponseCode.OpFailedStorageBusy) { |
| 3351 | rc = StorageResultCode.OperationFailedInternalError; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3352 | } |
| 3353 | } |
| 3354 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3355 | if (rc == StorageResultCode.OperationSucceeded) { |
| 3356 | if (DEBUG_OBB) |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 3357 | Slog.d(TAG, "Successfully mounted OBB " + mObbState.canonicalPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3358 | |
| 3359 | synchronized (mObbMounts) { |
| 3360 | addObbStateLocked(mObbState); |
| 3361 | } |
| 3362 | |
| 3363 | sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 3364 | } else { |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 3365 | Slog.e(TAG, "Couldn't mount OBB file: " + rc); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3366 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3367 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 3368 | } |
| 3369 | } |
| 3370 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3371 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3372 | public void handleError() { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3373 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 3374 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3375 | |
| 3376 | @Override |
| 3377 | public String toString() { |
| 3378 | StringBuilder sb = new StringBuilder(); |
| 3379 | sb.append("MountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3380 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3381 | sb.append('}'); |
| 3382 | return sb.toString(); |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | class UnmountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 3387 | private final boolean mForceUnmount; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3388 | |
| 3389 | UnmountObbAction(ObbState obbState, boolean force) { |
| 3390 | super(obbState); |
| 3391 | mForceUnmount = force; |
| 3392 | } |
| 3393 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3394 | @Override |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3395 | public void handleExecute() throws IOException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3396 | waitForReady(); |
| 3397 | warnOnNotMounted(); |
| 3398 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3399 | final ObbState existingState; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3400 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3401 | existingState = mObbPathToStateMap.get(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3402 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3403 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3404 | if (existingState == null) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3405 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED); |
| 3406 | return; |
| 3407 | } |
| 3408 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3409 | if (existingState.ownerGid != mObbState.ownerGid) { |
| 3410 | Slog.w(TAG, "Permission denied attempting to unmount OBB " + existingState.rawPath |
| 3411 | + " (owned by GID " + existingState.ownerGid + ")"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3412 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED); |
| 3413 | return; |
| 3414 | } |
| 3415 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3416 | int rc = StorageResultCode.OperationSucceeded; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3417 | try { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 3418 | final Command cmd = new Command("obb", "unmount", mObbState.canonicalPath); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 3419 | if (mForceUnmount) { |
| 3420 | cmd.appendArg("force"); |
| 3421 | } |
| 3422 | mConnector.execute(cmd); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3423 | } catch (NativeDaemonConnectorException e) { |
| 3424 | int code = e.getCode(); |
| 3425 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 3426 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 3427 | } else if (code == VoldResponseCode.OpFailedStorageNotFound) { |
| 3428 | // If it's not mounted then we've already won. |
| 3429 | rc = StorageResultCode.OperationSucceeded; |
| 3430 | } else { |
| 3431 | rc = StorageResultCode.OperationFailedInternalError; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3432 | } |
| 3433 | } |
| 3434 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3435 | if (rc == StorageResultCode.OperationSucceeded) { |
| 3436 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3437 | removeObbStateLocked(existingState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3438 | } |
| 3439 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3440 | sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3441 | } else { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3442 | Slog.w(TAG, "Could not unmount OBB: " + existingState); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3443 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3444 | } |
| 3445 | } |
| 3446 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 3447 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3448 | public void handleError() { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3449 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | @Override |
| 3453 | public String toString() { |
| 3454 | StringBuilder sb = new StringBuilder(); |
| 3455 | sb.append("UnmountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 3456 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3457 | sb.append(",force="); |
| 3458 | sb.append(mForceUnmount); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 3459 | sb.append('}'); |
| 3460 | return sb.toString(); |
| 3461 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 3462 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3463 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 3464 | private static class Callbacks extends Handler { |
| 3465 | private static final int MSG_STORAGE_STATE_CHANGED = 1; |
| 3466 | private static final int MSG_VOLUME_STATE_CHANGED = 2; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 3467 | private static final int MSG_VOLUME_RECORD_CHANGED = 3; |
| 3468 | private static final int MSG_VOLUME_FORGOTTEN = 4; |
| 3469 | private static final int MSG_DISK_SCANNED = 5; |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 3470 | private static final int MSG_DISK_DESTROYED = 6; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 3471 | |
| 3472 | private final RemoteCallbackList<IMountServiceListener> |
| 3473 | mCallbacks = new RemoteCallbackList<>(); |
| 3474 | |
| 3475 | public Callbacks(Looper looper) { |
| 3476 | super(looper); |
| 3477 | } |
| 3478 | |
| 3479 | public void register(IMountServiceListener callback) { |
| 3480 | mCallbacks.register(callback); |
| 3481 | } |
| 3482 | |
| 3483 | public void unregister(IMountServiceListener callback) { |
| 3484 | mCallbacks.unregister(callback); |
| 3485 | } |
| 3486 | |
| 3487 | @Override |
| 3488 | public void handleMessage(Message msg) { |
| 3489 | final SomeArgs args = (SomeArgs) msg.obj; |
| 3490 | final int n = mCallbacks.beginBroadcast(); |
| 3491 | for (int i = 0; i < n; i++) { |
| 3492 | final IMountServiceListener callback = mCallbacks.getBroadcastItem(i); |
| 3493 | try { |
| 3494 | invokeCallback(callback, msg.what, args); |
| 3495 | } catch (RemoteException ignored) { |
| 3496 | } |
| 3497 | } |
| 3498 | mCallbacks.finishBroadcast(); |
| 3499 | args.recycle(); |
| 3500 | } |
| 3501 | |
| 3502 | private void invokeCallback(IMountServiceListener callback, int what, SomeArgs args) |
| 3503 | throws RemoteException { |
| 3504 | switch (what) { |
| 3505 | case MSG_STORAGE_STATE_CHANGED: { |
| 3506 | callback.onStorageStateChanged((String) args.arg1, (String) args.arg2, |
| 3507 | (String) args.arg3); |
| 3508 | break; |
| 3509 | } |
| 3510 | case MSG_VOLUME_STATE_CHANGED: { |
| 3511 | callback.onVolumeStateChanged((VolumeInfo) args.arg1, args.argi2, args.argi3); |
| 3512 | break; |
| 3513 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 3514 | case MSG_VOLUME_RECORD_CHANGED: { |
| 3515 | callback.onVolumeRecordChanged((VolumeRecord) args.arg1); |
| 3516 | break; |
| 3517 | } |
| 3518 | case MSG_VOLUME_FORGOTTEN: { |
| 3519 | callback.onVolumeForgotten((String) args.arg1); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 3520 | break; |
| 3521 | } |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 3522 | case MSG_DISK_SCANNED: { |
| 3523 | callback.onDiskScanned((DiskInfo) args.arg1, args.argi2); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 3524 | break; |
| 3525 | } |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 3526 | case MSG_DISK_DESTROYED: { |
| 3527 | callback.onDiskDestroyed((DiskInfo) args.arg1); |
| 3528 | break; |
| 3529 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | private void notifyStorageStateChanged(String path, String oldState, String newState) { |
| 3534 | final SomeArgs args = SomeArgs.obtain(); |
| 3535 | args.arg1 = path; |
| 3536 | args.arg2 = oldState; |
| 3537 | args.arg3 = newState; |
| 3538 | obtainMessage(MSG_STORAGE_STATE_CHANGED, args).sendToTarget(); |
| 3539 | } |
| 3540 | |
| 3541 | private void notifyVolumeStateChanged(VolumeInfo vol, int oldState, int newState) { |
| 3542 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 3543 | args.arg1 = vol.clone(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 3544 | args.argi2 = oldState; |
| 3545 | args.argi3 = newState; |
| 3546 | obtainMessage(MSG_VOLUME_STATE_CHANGED, args).sendToTarget(); |
| 3547 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 3548 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 3549 | private void notifyVolumeRecordChanged(VolumeRecord rec) { |
| 3550 | final SomeArgs args = SomeArgs.obtain(); |
| 3551 | args.arg1 = rec.clone(); |
| 3552 | obtainMessage(MSG_VOLUME_RECORD_CHANGED, args).sendToTarget(); |
| 3553 | } |
| 3554 | |
| 3555 | private void notifyVolumeForgotten(String fsUuid) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 3556 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 3557 | args.arg1 = fsUuid; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 3558 | obtainMessage(MSG_VOLUME_FORGOTTEN, args).sendToTarget(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 3559 | } |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 3560 | |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 3561 | private void notifyDiskScanned(DiskInfo disk, int volumeCount) { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 3562 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 3563 | args.arg1 = disk.clone(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 3564 | args.argi2 = volumeCount; |
| 3565 | obtainMessage(MSG_DISK_SCANNED, args).sendToTarget(); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 3566 | } |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 3567 | |
| 3568 | private void notifyDiskDestroyed(DiskInfo disk) { |
| 3569 | final SomeArgs args = SomeArgs.obtain(); |
| 3570 | args.arg1 = disk.clone(); |
| 3571 | obtainMessage(MSG_DISK_DESTROYED, args).sendToTarget(); |
| 3572 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 3573 | } |
| 3574 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3575 | @Override |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3576 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 3577 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 3578 | |
| 3579 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ", 160); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 3580 | synchronized (mLock) { |
| 3581 | pw.println("Disks:"); |
| 3582 | pw.increaseIndent(); |
| 3583 | for (int i = 0; i < mDisks.size(); i++) { |
| 3584 | final DiskInfo disk = mDisks.valueAt(i); |
| 3585 | disk.dump(pw); |
| 3586 | } |
| 3587 | pw.decreaseIndent(); |
| 3588 | |
| 3589 | pw.println(); |
| 3590 | pw.println("Volumes:"); |
| 3591 | pw.increaseIndent(); |
| 3592 | for (int i = 0; i < mVolumes.size(); i++) { |
| 3593 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 3594 | if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) continue; |
| 3595 | vol.dump(pw); |
| 3596 | } |
| 3597 | pw.decreaseIndent(); |
| 3598 | |
| 3599 | pw.println(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 3600 | pw.println("Records:"); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 3601 | pw.increaseIndent(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 3602 | for (int i = 0; i < mRecords.size(); i++) { |
| 3603 | final VolumeRecord note = mRecords.valueAt(i); |
| 3604 | note.dump(pw); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 3605 | } |
| 3606 | pw.decreaseIndent(); |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 3607 | |
| 3608 | pw.println(); |
| 3609 | pw.println("Primary storage UUID: " + mPrimaryStorageUuid); |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 3610 | pw.println("Force adoptable: " + mForceAdoptable); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 3611 | pw.println(); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 3612 | pw.println("Local unlocked users: " + Arrays.toString(mLocalUnlockedUsers)); |
| 3613 | pw.println("System unlocked users: " + Arrays.toString(mSystemUnlockedUsers)); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 3614 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3615 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3616 | synchronized (mObbMounts) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 3617 | pw.println(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3618 | pw.println("mObbMounts:"); |
| 3619 | pw.increaseIndent(); |
| 3620 | final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet() |
| 3621 | .iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3622 | while (binders.hasNext()) { |
| 3623 | Entry<IBinder, List<ObbState>> e = binders.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3624 | pw.println(e.getKey() + ":"); |
| 3625 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3626 | final List<ObbState> obbStates = e.getValue(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3627 | for (final ObbState obbState : obbStates) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3628 | pw.println(obbState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3629 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3630 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3631 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3632 | pw.decreaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3633 | |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3634 | pw.println(); |
| 3635 | pw.println("mObbPathToStateMap:"); |
| 3636 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3637 | final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator(); |
| 3638 | while (maps.hasNext()) { |
| 3639 | final Entry<String, ObbState> e = maps.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3640 | pw.print(e.getKey()); |
| 3641 | pw.print(" -> "); |
| 3642 | pw.println(e.getValue()); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 3643 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3644 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3645 | } |
Kenny Root | 4161f9b | 2011-07-13 09:48:33 -0700 | [diff] [blame] | 3646 | |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 3647 | pw.println(); |
Jeff Sharkey | 5b0e520 | 2015-12-18 17:18:09 -0700 | [diff] [blame] | 3648 | pw.println("mConnector:"); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3649 | pw.increaseIndent(); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 3650 | mConnector.dump(fd, pw, args); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 3651 | pw.decreaseIndent(); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 3652 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 3653 | pw.println(); |
Jeff Sharkey | 5b0e520 | 2015-12-18 17:18:09 -0700 | [diff] [blame] | 3654 | pw.println("mCryptConnector:"); |
| 3655 | pw.increaseIndent(); |
| 3656 | mCryptConnector.dump(fd, pw, args); |
| 3657 | pw.decreaseIndent(); |
| 3658 | |
| 3659 | pw.println(); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 3660 | pw.print("Last maintenance: "); |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 3661 | pw.println(TimeUtils.formatForLogging(mLastMaintenance)); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 3662 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3663 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 3664 | /** {@inheritDoc} */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 3665 | @Override |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 3666 | public void monitor() { |
| 3667 | if (mConnector != null) { |
| 3668 | mConnector.monitor(); |
| 3669 | } |
Paul Lawrence | 1c62cbb | 2015-06-03 14:14:52 -0700 | [diff] [blame] | 3670 | if (mCryptConnector != null) { |
| 3671 | mCryptConnector.monitor(); |
| 3672 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 3673 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 3674 | |
| 3675 | private final class MountServiceInternalImpl extends MountServiceInternal { |
| 3676 | // Not guarded by a lock. |
| 3677 | private final CopyOnWriteArrayList<ExternalStorageMountPolicy> mPolicies = |
| 3678 | new CopyOnWriteArrayList<>(); |
| 3679 | |
| 3680 | @Override |
| 3681 | public void addExternalStoragePolicy(ExternalStorageMountPolicy policy) { |
| 3682 | // No locking - CopyOnWriteArrayList |
| 3683 | mPolicies.add(policy); |
| 3684 | } |
| 3685 | |
| 3686 | @Override |
| 3687 | public void onExternalStoragePolicyChanged(int uid, String packageName) { |
| 3688 | final int mountMode = getExternalStorageMountMode(uid, packageName); |
| 3689 | remountUidExternalStorage(uid, mountMode); |
| 3690 | } |
| 3691 | |
| 3692 | @Override |
| 3693 | public int getExternalStorageMountMode(int uid, String packageName) { |
| 3694 | // No locking - CopyOnWriteArrayList |
| 3695 | int mountMode = Integer.MAX_VALUE; |
| 3696 | for (ExternalStorageMountPolicy policy : mPolicies) { |
| 3697 | final int policyMode = policy.getMountMode(uid, packageName); |
| 3698 | if (policyMode == Zygote.MOUNT_EXTERNAL_NONE) { |
| 3699 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 3700 | } |
| 3701 | mountMode = Math.min(mountMode, policyMode); |
| 3702 | } |
| 3703 | if (mountMode == Integer.MAX_VALUE) { |
| 3704 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 3705 | } |
| 3706 | return mountMode; |
| 3707 | } |
| 3708 | |
| 3709 | public boolean hasExternalStorage(int uid, String packageName) { |
Amith Yamasani | 2bd5cff | 2015-07-22 14:42:31 -0700 | [diff] [blame] | 3710 | // No need to check for system uid. This avoids a deadlock between |
| 3711 | // PackageManagerService and AppOpsService. |
| 3712 | if (uid == Process.SYSTEM_UID) { |
| 3713 | return true; |
| 3714 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 3715 | // No locking - CopyOnWriteArrayList |
| 3716 | for (ExternalStorageMountPolicy policy : mPolicies) { |
| 3717 | final boolean policyHasStorage = policy.hasExternalStorage(uid, packageName); |
| 3718 | if (!policyHasStorage) { |
| 3719 | return false; |
| 3720 | } |
| 3721 | } |
| 3722 | return true; |
| 3723 | } |
| 3724 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 3725 | } |