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 | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 19 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
| 20 | import static com.android.internal.util.XmlUtils.readStringAttribute; |
| 21 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
| 22 | import static com.android.internal.util.XmlUtils.writeStringAttribute; |
| 23 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 24 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
| 25 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 26 | import android.Manifest; |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 27 | import android.app.ActivityManagerNative; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 28 | import android.app.AppOpsManager; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 29 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 32 | import android.content.ServiceConnection; |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 33 | import android.content.res.Configuration; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 34 | import android.content.res.ObbInfo; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 35 | import android.mtp.MtpStorage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.net.Uri; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 37 | import android.os.Binder; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 38 | import android.os.Environment; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 39 | import android.os.Environment.UserEnvironment; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 40 | import android.os.FileUtils; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 41 | import android.os.Handler; |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 42 | import android.os.HandlerThread; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 43 | import android.os.IBinder; |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 44 | import android.os.Looper; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 45 | import android.os.Message; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 46 | import android.os.RemoteCallbackList; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 47 | import android.os.RemoteException; |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 48 | import android.os.ServiceManager; |
Svetoslav | f23b64d | 2013-04-25 14:45:54 -0700 | [diff] [blame] | 49 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 51 | import android.os.UserHandle; |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 52 | import android.os.UserManager; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 53 | import android.os.storage.DiskInfo; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 54 | import android.os.storage.IMountService; |
| 55 | import android.os.storage.IMountServiceListener; |
| 56 | import android.os.storage.IMountShutdownObserver; |
| 57 | import android.os.storage.IObbActionListener; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 58 | import android.os.storage.OnObbStateChangeListener; |
Paul Lawrence | 46791e7 | 2014-04-03 09:10:26 -0700 | [diff] [blame] | 59 | import android.os.storage.StorageManager; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 60 | import android.os.storage.StorageResultCode; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 61 | import android.os.storage.StorageVolume; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 62 | import android.os.storage.VolumeInfo; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 63 | import android.text.TextUtils; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 64 | import android.util.ArrayMap; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 65 | import android.util.AtomicFile; |
| 66 | import android.util.DebugUtils; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 67 | import android.util.Log; |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 68 | import android.util.Slog; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 69 | import android.util.Xml; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 70 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 71 | import libcore.io.IoUtils; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 72 | import libcore.util.EmptyArray; |
| 73 | import libcore.util.HexEncoding; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 74 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 75 | import com.android.internal.annotations.GuardedBy; |
| 76 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 77 | import com.android.internal.app.IMediaContainerService; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 78 | import com.android.internal.os.SomeArgs; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 79 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 80 | import com.android.internal.util.FastXmlSerializer; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 81 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 82 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 83 | import com.android.server.NativeDaemonConnector.Command; |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 84 | import com.android.server.NativeDaemonConnector.SensitiveArg; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 85 | import com.android.server.pm.PackageManagerService; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 86 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 87 | import org.xmlpull.v1.XmlPullParser; |
| 88 | import org.xmlpull.v1.XmlPullParserException; |
| 89 | import org.xmlpull.v1.XmlSerializer; |
| 90 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 91 | import java.io.File; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 92 | import java.io.FileDescriptor; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 93 | import java.io.FileInputStream; |
| 94 | import java.io.FileNotFoundException; |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 95 | import java.io.FileOutputStream; |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 96 | import java.io.IOException; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 97 | import java.io.PrintWriter; |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 98 | import java.math.BigInteger; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 99 | import java.nio.charset.StandardCharsets; |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 100 | import java.security.NoSuchAlgorithmException; |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 101 | import java.security.spec.InvalidKeySpecException; |
| 102 | import java.security.spec.KeySpec; |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 103 | import java.text.SimpleDateFormat; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 104 | import java.util.ArrayList; |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 105 | import java.util.Date; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 106 | import java.util.HashMap; |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 107 | import java.util.HashSet; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 108 | import java.util.Iterator; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 109 | import java.util.LinkedList; |
| 110 | import java.util.List; |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 111 | import java.util.Locale; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 112 | import java.util.Map; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 113 | import java.util.Map.Entry; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 114 | import java.util.Objects; |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 115 | import java.util.concurrent.CountDownLatch; |
| 116 | import java.util.concurrent.TimeUnit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 118 | import javax.crypto.SecretKey; |
| 119 | import javax.crypto.SecretKeyFactory; |
| 120 | import javax.crypto.spec.PBEKeySpec; |
| 121 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | /** |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 123 | * Service responsible for various storage media. Connects to {@code vold} to |
| 124 | * watch for and manage dynamically added storage, such as SD cards and USB mass |
| 125 | * 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] | 126 | */ |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 127 | class MountService extends IMountService.Stub |
| 128 | implements INativeDaemonConnectorCallbacks, Watchdog.Monitor { |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 129 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 130 | // TODO: finish enforcing UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA |
| 131 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 132 | // Static direct instance pointer for the tightly-coupled idle service to use |
| 133 | static MountService sSelf = null; |
| 134 | |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 135 | public static class Lifecycle extends SystemService { |
| 136 | private MountService mMountService; |
| 137 | |
| 138 | public Lifecycle(Context context) { |
| 139 | super(context); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void onStart() { |
| 144 | mMountService = new MountService(getContext()); |
| 145 | publishBinderService("mount", mMountService); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onBootPhase(int phase) { |
| 150 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 151 | mMountService.systemReady(); |
| 152 | } |
| 153 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 154 | |
| 155 | @Override |
| 156 | public void onStartUser(int userHandle) { |
| 157 | mMountService.onStartUser(userHandle); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void onCleanupUser(int userHandle) { |
| 162 | mMountService.onCleanupUser(userHandle); |
| 163 | } |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 166 | private static final boolean LOCAL_LOGD = false; |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 167 | private static final boolean DEBUG_EVENTS = false; |
Kenny Root | b7db272 | 2011-01-25 16:39:35 -0800 | [diff] [blame] | 168 | private static final boolean DEBUG_OBB = false; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 169 | |
Kenny Root | 07714d4 | 2011-08-17 17:49:28 -0700 | [diff] [blame] | 170 | // Disable this since it messes up long-running cryptfs operations. |
| 171 | private static final boolean WATCHDOG_ENABLE = false; |
| 172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | private static final String TAG = "MountService"; |
| 174 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 175 | private static final String VOLD_TAG = "VoldConnector"; |
| 176 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 177 | /** Maximum number of ASEC containers allowed to be mounted. */ |
| 178 | private static final int MAX_CONTAINERS = 250; |
| 179 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 180 | /* |
| 181 | * Internal vold response code constants |
| 182 | */ |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 183 | class VoldResponseCode { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 184 | /* |
| 185 | * 100 series - Requestion action was initiated; expect another reply |
| 186 | * before proceeding with a new command. |
| 187 | */ |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 188 | public static final int VolumeListResult = 110; |
| 189 | public static final int AsecListResult = 111; |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 190 | public static final int StorageUsersListResult = 112; |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 191 | public static final int CryptfsGetfieldResult = 113; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 192 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 193 | /* |
| 194 | * 200 series - Requestion action has been successfully completed. |
| 195 | */ |
| 196 | public static final int ShareStatusResult = 210; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 197 | public static final int AsecPathResult = 211; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 198 | public static final int ShareEnabledResult = 212; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 199 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 200 | /* |
| 201 | * 400 series - Command was accepted, but the requested action |
| 202 | * did not take place. |
| 203 | */ |
| 204 | public static final int OpFailedNoMedia = 401; |
| 205 | public static final int OpFailedMediaBlank = 402; |
| 206 | public static final int OpFailedMediaCorrupt = 403; |
| 207 | public static final int OpFailedVolNotMounted = 404; |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 208 | public static final int OpFailedStorageBusy = 405; |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 209 | public static final int OpFailedStorageNotFound = 406; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * 600 series - Unsolicited broadcasts. |
| 213 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 214 | public static final int DISK_CREATED = 640; |
| 215 | public static final int DISK_SIZE_CHANGED = 641; |
| 216 | public static final int DISK_LABEL_CHANGED = 642; |
| 217 | public static final int DISK_VOLUME_CREATED = 643; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 218 | public static final int DISK_VOLUME_DESTROYED = 644; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 219 | public static final int DISK_DESTROYED = 649; |
| 220 | |
| 221 | public static final int VOLUME_CREATED = 650; |
| 222 | public static final int VOLUME_STATE_CHANGED = 651; |
| 223 | public static final int VOLUME_FS_TYPE_CHANGED = 652; |
| 224 | public static final int VOLUME_FS_UUID_CHANGED = 653; |
| 225 | public static final int VOLUME_FS_LABEL_CHANGED = 654; |
| 226 | public static final int VOLUME_PATH_CHANGED = 655; |
| 227 | public static final int VOLUME_DESTROYED = 659; |
Svetoslav | f23b64d | 2013-04-25 14:45:54 -0700 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * 700 series - fstrim |
| 231 | */ |
| 232 | public static final int FstrimCompleted = 700; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 235 | private static final String TAG_VOLUMES = "volumes"; |
| 236 | private static final String TAG_VOLUME = "volume"; |
| 237 | private static final String ATTR_TYPE = "type"; |
| 238 | private static final String ATTR_FS_UUID = "fsUuid"; |
| 239 | private static final String ATTR_NICKNAME = "nickname"; |
| 240 | private static final String ATTR_USER_FLAGS = "userFlags"; |
| 241 | |
| 242 | private final AtomicFile mMetadataFile; |
| 243 | |
| 244 | private static class VolumeMetadata { |
| 245 | public final int type; |
| 246 | public final String fsUuid; |
| 247 | public String nickname; |
| 248 | public int userFlags; |
| 249 | |
| 250 | public VolumeMetadata(int type, String fsUuid) { |
| 251 | this.type = type; |
| 252 | this.fsUuid = Preconditions.checkNotNull(fsUuid); |
| 253 | } |
| 254 | |
| 255 | public static VolumeMetadata read(XmlPullParser in) throws IOException { |
| 256 | final int type = readIntAttribute(in, ATTR_TYPE); |
| 257 | final String fsUuid = readStringAttribute(in, ATTR_FS_UUID); |
| 258 | final VolumeMetadata meta = new VolumeMetadata(type, fsUuid); |
| 259 | meta.nickname = readStringAttribute(in, ATTR_NICKNAME); |
| 260 | meta.userFlags = readIntAttribute(in, ATTR_USER_FLAGS); |
| 261 | return meta; |
| 262 | } |
| 263 | |
| 264 | public static void write(XmlSerializer out, VolumeMetadata meta) throws IOException { |
| 265 | out.startTag(null, TAG_VOLUME); |
| 266 | writeIntAttribute(out, ATTR_TYPE, meta.type); |
| 267 | writeStringAttribute(out, ATTR_FS_UUID, meta.fsUuid); |
| 268 | writeStringAttribute(out, ATTR_NICKNAME, meta.nickname); |
| 269 | writeIntAttribute(out, ATTR_USER_FLAGS, meta.userFlags); |
| 270 | out.endTag(null, TAG_VOLUME); |
| 271 | } |
| 272 | |
| 273 | public void dump(IndentingPrintWriter pw) { |
| 274 | pw.println("VolumeMetadata:"); |
| 275 | pw.increaseIndent(); |
| 276 | pw.printPair("type", DebugUtils.valueToString(VolumeInfo.class, "TYPE_", type)); |
| 277 | pw.printPair("fsUuid", fsUuid); |
| 278 | pw.printPair("nickname", nickname); |
| 279 | pw.printPair("userFlags", |
| 280 | DebugUtils.flagsToString(VolumeInfo.class, "USER_FLAG_", userFlags)); |
| 281 | pw.decreaseIndent(); |
| 282 | pw.println(); |
| 283 | } |
| 284 | } |
| 285 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 286 | /** |
| 287 | * <em>Never</em> hold the lock while performing downcalls into vold, since |
| 288 | * unsolicited events can suddenly appear to update data structures. |
| 289 | */ |
| 290 | private final Object mLock = new Object(); |
| 291 | |
| 292 | @GuardedBy("mLock") |
| 293 | private int[] mStartedUsers = EmptyArray.INT; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 294 | |
| 295 | /** Map from disk ID to disk */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 296 | @GuardedBy("mLock") |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 297 | private ArrayMap<String, DiskInfo> mDisks = new ArrayMap<>(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 298 | /** Map from volume 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, VolumeInfo> mVolumes = new ArrayMap<>(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 301 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 302 | /** Map from UUID to metadata */ |
| 303 | @GuardedBy("mLock") |
| 304 | private ArrayMap<String, VolumeMetadata> mMetadata = new ArrayMap<>(); |
| 305 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 306 | private DiskInfo findDiskById(String id) { |
| 307 | synchronized (mLock) { |
| 308 | final DiskInfo disk = mDisks.get(id); |
| 309 | if (disk != null) { |
| 310 | return disk; |
| 311 | } |
| 312 | } |
| 313 | throw new IllegalArgumentException("No disk found for ID " + id); |
| 314 | } |
| 315 | |
| 316 | private VolumeInfo findVolumeById(String id) { |
| 317 | synchronized (mLock) { |
| 318 | final VolumeInfo vol = mVolumes.get(id); |
| 319 | if (vol != null) { |
| 320 | return vol; |
| 321 | } |
| 322 | } |
| 323 | throw new IllegalArgumentException("No volume found for ID " + id); |
| 324 | } |
| 325 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 326 | @Deprecated |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 327 | private String findVolumeIdForPath(String path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 328 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 329 | for (int i = 0; i < mVolumes.size(); i++) { |
| 330 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 331 | if (vol.path != null && path.startsWith(vol.path)) { |
| 332 | return vol.id; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 336 | throw new IllegalArgumentException("No volume found for path " + path); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 339 | private VolumeMetadata findOrCreateMetadataLocked(VolumeInfo vol) { |
| 340 | VolumeMetadata meta = mMetadata.get(vol.fsUuid); |
| 341 | if (meta == null) { |
| 342 | meta = new VolumeMetadata(vol.type, vol.fsUuid); |
| 343 | mMetadata.put(meta.fsUuid, meta); |
| 344 | } |
| 345 | return meta; |
| 346 | } |
| 347 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 348 | private static int sNextMtpIndex = 1; |
| 349 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 350 | private static int allocateMtpIndex(String volId) { |
| 351 | if (VolumeInfo.ID_EMULATED_INTERNAL.equals(volId)) { |
| 352 | return 0; |
| 353 | } else { |
| 354 | return sNextMtpIndex++; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 358 | /** List of crypto types. |
| 359 | * These must match CRYPT_TYPE_XXX in cryptfs.h AND their |
| 360 | * corresponding commands in CommandListener.cpp */ |
| 361 | public static final String[] CRYPTO_TYPES |
| 362 | = { "password", "default", "pattern", "pin" }; |
| 363 | |
Brian Carlstrom | 7395a8a | 2014-04-28 22:11:01 -0700 | [diff] [blame] | 364 | private final Context mContext; |
Brian Carlstrom | dfad99a | 2014-05-07 15:21:14 -0700 | [diff] [blame] | 365 | private final NativeDaemonConnector mConnector; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 366 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 367 | private volatile boolean mSystemReady = false; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 368 | private volatile boolean mDaemonConnected = false; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 369 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 370 | private PackageManagerService mPms; |
| 371 | |
| 372 | private final Callbacks mCallbacks; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 373 | |
Jeff Sharkey | 0be607c | 2012-11-14 14:39:19 -0800 | [diff] [blame] | 374 | private final CountDownLatch mConnectedSignal = new CountDownLatch(1); |
| 375 | private final CountDownLatch mAsecsScanned = new CountDownLatch(1); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 376 | |
| 377 | private final Object mUnmountLock = new Object(); |
| 378 | @GuardedBy("mUnmountLock") |
| 379 | private CountDownLatch mUnmountSignal; |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 380 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 381 | /** |
| 382 | * Private hash of currently mounted secure containers. |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 383 | * Used as a lock in methods to manipulate secure containers. |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 384 | */ |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 385 | final private HashSet<String> mAsecMountSet = new HashSet<String>(); |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 386 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 387 | /** |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 388 | * The size of the crypto algorithm key in bits for OBB files. Currently |
| 389 | * Twofish is used which takes 128-bit keys. |
| 390 | */ |
| 391 | private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128; |
| 392 | |
| 393 | /** |
| 394 | * The number of times to run SHA1 in the PBKDF2 function for OBB files. |
| 395 | * 1024 is reasonably secure and not too slow. |
| 396 | */ |
| 397 | private static final int PBKDF2_HASH_ROUNDS = 1024; |
| 398 | |
| 399 | /** |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 400 | * Mounted OBB tracking information. Used to track the current state of all |
| 401 | * OBBs. |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 402 | */ |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 403 | final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 404 | |
| 405 | /** Map from raw paths to {@link ObbState}. */ |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 406 | final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>(); |
| 407 | |
| 408 | class ObbState implements IBinder.DeathRecipient { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 409 | public ObbState(String rawPath, String canonicalPath, int callingUid, |
| 410 | IObbActionListener token, int nonce) { |
| 411 | this.rawPath = rawPath; |
| 412 | this.canonicalPath = canonicalPath.toString(); |
| 413 | |
| 414 | final int userId = UserHandle.getUserId(callingUid); |
| 415 | this.ownerPath = buildObbPath(canonicalPath, userId, false); |
| 416 | this.voldPath = buildObbPath(canonicalPath, userId, true); |
| 417 | |
| 418 | this.ownerGid = UserHandle.getSharedAppGid(callingUid); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 419 | this.token = token; |
| 420 | this.nonce = nonce; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 423 | final String rawPath; |
| 424 | final String canonicalPath; |
| 425 | final String ownerPath; |
| 426 | final String voldPath; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 427 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 428 | final int ownerGid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 429 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 430 | // Token of remote Binder caller |
| 431 | final IObbActionListener token; |
| 432 | |
| 433 | // Identifier to pass back to the token |
| 434 | final int nonce; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 435 | |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 436 | public IBinder getBinder() { |
| 437 | return token.asBinder(); |
| 438 | } |
| 439 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 440 | @Override |
| 441 | public void binderDied() { |
| 442 | ObbAction action = new UnmountObbAction(this, true); |
| 443 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 444 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 445 | |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 446 | public void link() throws RemoteException { |
| 447 | getBinder().linkToDeath(this, 0); |
| 448 | } |
| 449 | |
| 450 | public void unlink() { |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 451 | getBinder().unlinkToDeath(this, 0); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 452 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 453 | |
| 454 | @Override |
| 455 | public String toString() { |
| 456 | StringBuilder sb = new StringBuilder("ObbState{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 457 | sb.append("rawPath=").append(rawPath); |
| 458 | sb.append(",canonicalPath=").append(canonicalPath); |
| 459 | sb.append(",ownerPath=").append(ownerPath); |
| 460 | sb.append(",voldPath=").append(voldPath); |
| 461 | sb.append(",ownerGid=").append(ownerGid); |
| 462 | sb.append(",token=").append(token); |
| 463 | sb.append(",binder=").append(getBinder()); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 464 | sb.append('}'); |
| 465 | return sb.toString(); |
| 466 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // OBB Action Handler |
| 470 | final private ObbActionHandler mObbActionHandler; |
| 471 | |
| 472 | // OBB action handler messages |
| 473 | private static final int OBB_RUN_ACTION = 1; |
| 474 | private static final int OBB_MCS_BOUND = 2; |
| 475 | private static final int OBB_MCS_UNBIND = 3; |
| 476 | private static final int OBB_MCS_RECONNECT = 4; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 477 | private static final int OBB_FLUSH_MOUNT_STATE = 5; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * Default Container Service information |
| 481 | */ |
| 482 | static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName( |
| 483 | "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService"); |
| 484 | |
| 485 | final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection(); |
| 486 | |
| 487 | class DefaultContainerConnection implements ServiceConnection { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 488 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 489 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 490 | if (DEBUG_OBB) |
| 491 | Slog.i(TAG, "onServiceConnected"); |
| 492 | IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service); |
| 493 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs)); |
| 494 | } |
| 495 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 496 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 497 | public void onServiceDisconnected(ComponentName name) { |
| 498 | if (DEBUG_OBB) |
| 499 | Slog.i(TAG, "onServiceDisconnected"); |
| 500 | } |
| 501 | }; |
| 502 | |
| 503 | // Used in the ObbActionHandler |
| 504 | private IMediaContainerService mContainerService = null; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 505 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 506 | // Last fstrim operation tracking |
| 507 | private static final String LAST_FSTRIM_FILE = "last-fstrim"; |
| 508 | private final File mLastMaintenanceFile; |
| 509 | private long mLastMaintenance; |
| 510 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 511 | // Handler messages |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 512 | private static final int H_SYSTEM_READY = 1; |
| 513 | private static final int H_DAEMON_CONNECTED = 2; |
| 514 | private static final int H_SHUTDOWN = 3; |
| 515 | private static final int H_FSTRIM = 4; |
| 516 | private static final int H_VOLUME_MOUNT = 5; |
| 517 | private static final int H_VOLUME_BROADCAST = 6; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 518 | |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 519 | class MountServiceHandler extends Handler { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 520 | public MountServiceHandler(Looper looper) { |
| 521 | super(looper); |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 524 | @Override |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 525 | public void handleMessage(Message msg) { |
| 526 | switch (msg.what) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 527 | case H_SYSTEM_READY: { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 528 | handleSystemReady(); |
| 529 | break; |
| 530 | } |
| 531 | case H_DAEMON_CONNECTED: { |
| 532 | handleDaemonConnected(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 533 | break; |
| 534 | } |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 535 | case H_FSTRIM: { |
| 536 | waitForReady(); |
| 537 | Slog.i(TAG, "Running fstrim idle maintenance"); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 538 | |
| 539 | // Remember when we kicked it off |
| 540 | try { |
| 541 | mLastMaintenance = System.currentTimeMillis(); |
| 542 | mLastMaintenanceFile.setLastModified(mLastMaintenance); |
| 543 | } catch (Exception e) { |
| 544 | Slog.e(TAG, "Unable to record last fstrim!"); |
| 545 | } |
| 546 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 547 | try { |
| 548 | // This method must be run on the main (handler) thread, |
| 549 | // so it is safe to directly call into vold. |
| 550 | mConnector.execute("fstrim", "dotrim"); |
| 551 | EventLogTags.writeFstrimStart(SystemClock.elapsedRealtime()); |
| 552 | } catch (NativeDaemonConnectorException ndce) { |
| 553 | Slog.e(TAG, "Failed to run fstrim!"); |
| 554 | } |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 555 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 556 | // invoke the completion callback, if any |
| 557 | Runnable callback = (Runnable) msg.obj; |
| 558 | if (callback != null) { |
| 559 | callback.run(); |
| 560 | } |
| 561 | break; |
| 562 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 563 | case H_SHUTDOWN: { |
| 564 | final IMountShutdownObserver obs = (IMountShutdownObserver) msg.obj; |
| 565 | boolean success = false; |
| 566 | try { |
| 567 | success = mConnector.execute("volume", "shutdown").isClassOk(); |
| 568 | } catch (NativeDaemonConnectorException ignored) { |
| 569 | } |
| 570 | if (obs != null) { |
| 571 | try { |
| 572 | obs.onShutDownComplete(success ? 0 : -1); |
| 573 | } catch (RemoteException ignored) { |
| 574 | } |
| 575 | } |
| 576 | break; |
| 577 | } |
| 578 | case H_VOLUME_MOUNT: { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 579 | final VolumeInfo vol = (VolumeInfo) msg.obj; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 580 | try { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 581 | mConnector.execute("volume", "mount", vol.id, vol.flags, vol.userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 582 | } catch (NativeDaemonConnectorException ignored) { |
| 583 | } |
| 584 | break; |
| 585 | } |
| 586 | case H_VOLUME_BROADCAST: { |
| 587 | final StorageVolume userVol = (StorageVolume) msg.obj; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 588 | final String envState = userVol.getState(); |
| 589 | Slog.d(TAG, "Volume " + userVol.getId() + " broadcasting " + envState + " to " |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 590 | + userVol.getOwner()); |
| 591 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 592 | final String action = VolumeInfo.getBroadcastForEnvironment(envState); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 593 | if (action != null) { |
| 594 | final Intent intent = new Intent(action, |
| 595 | Uri.fromFile(userVol.getPathFile())); |
| 596 | intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, userVol); |
| 597 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 598 | mContext.sendBroadcastAsUser(intent, userVol.getOwner()); |
| 599 | } |
| 600 | break; |
| 601 | } |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 602 | } |
| 603 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 604 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 605 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 606 | private final Handler mHandler; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 607 | |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 608 | @Override |
| 609 | public void waitForAsecScan() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 610 | waitForLatch(mAsecsScanned, "mAsecsScanned"); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 611 | } |
| 612 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 613 | private void waitForReady() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 614 | waitForLatch(mConnectedSignal, "mConnectedSignal"); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 617 | private void waitForLatch(CountDownLatch latch, String condition) { |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 618 | for (;;) { |
| 619 | try { |
| 620 | if (latch.await(5000, TimeUnit.MILLISECONDS)) { |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 621 | return; |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 622 | } else { |
| 623 | Slog.w(TAG, "Thread " + Thread.currentThread().getName() |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 624 | + " still waiting for " + condition + "..."); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 625 | } |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 626 | } catch (InterruptedException e) { |
| 627 | Slog.w(TAG, "Interrupt while waiting for MountService to be ready."); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 628 | } |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 629 | } |
San Mehat | 1f6301e | 2010-01-07 22:40:27 -0800 | [diff] [blame] | 630 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 631 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 632 | private boolean isReady() { |
| 633 | try { |
| 634 | return mConnectedSignal.await(0, TimeUnit.MILLISECONDS); |
| 635 | } catch (InterruptedException e) { |
| 636 | return false; |
| 637 | } |
| 638 | } |
| 639 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 640 | private void handleSystemReady() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 641 | resetIfReadyAndConnected(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 642 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 643 | // Start scheduling nominally-daily fstrim operations |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 644 | MountServiceIdler.scheduleIdlePass(mContext); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 647 | private void resetIfReadyAndConnected() { |
| 648 | Slog.d(TAG, "Thinking about reset, mSystemReady=" + mSystemReady |
| 649 | + ", mDaemonConnected=" + mDaemonConnected); |
| 650 | if (mSystemReady && mDaemonConnected) { |
| 651 | mDisks.clear(); |
| 652 | mVolumes.clear(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 653 | |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 654 | // Create a stub volume that represents internal storage |
| 655 | final VolumeInfo internal = new VolumeInfo(VolumeInfo.ID_PRIVATE_INTERNAL, |
| 656 | VolumeInfo.TYPE_PRIVATE, 0); |
| 657 | internal.state = VolumeInfo.STATE_MOUNTED; |
| 658 | internal.path = Environment.getDataDirectory().getAbsolutePath(); |
| 659 | mVolumes.put(internal.id, internal); |
| 660 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 661 | try { |
| 662 | mConnector.execute("volume", "reset"); |
| 663 | } catch (NativeDaemonConnectorException e) { |
| 664 | Slog.w(TAG, "Failed to reset vold", e); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 665 | } |
| 666 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 667 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 668 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 669 | private void onStartUser(int userId) { |
| 670 | Slog.d(TAG, "onStartUser " + userId); |
| 671 | |
| 672 | // We purposefully block here to make sure that user-specific |
| 673 | // staging area is ready so it's ready for zygote-forked apps to |
| 674 | // bind mount against. |
| 675 | try { |
| 676 | mConnector.execute("volume", "start_user", userId); |
| 677 | } catch (NativeDaemonConnectorException ignored) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 678 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 679 | |
| 680 | // Record user as started so newly mounted volumes kick off events |
| 681 | // correctly, then synthesize events for any already-mounted volumes. |
| 682 | synchronized (mVolumes) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 683 | for (int i = 0; i < mVolumes.size(); i++) { |
| 684 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 685 | if (vol.isVisibleToUser(userId) && vol.state == VolumeInfo.STATE_MOUNTED) { |
| 686 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 687 | mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 688 | |
| 689 | mCallbacks.notifyStorageStateChanged(userVol.getPath(), |
| 690 | Environment.MEDIA_MOUNTED, Environment.MEDIA_MOUNTED); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | mStartedUsers = ArrayUtils.appendInt(mStartedUsers, userId); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | private void onCleanupUser(int userId) { |
| 698 | Slog.d(TAG, "onCleanupUser " + userId); |
| 699 | |
| 700 | try { |
| 701 | mConnector.execute("volume", "cleanup_user", userId); |
| 702 | } catch (NativeDaemonConnectorException ignored) { |
| 703 | } |
| 704 | |
| 705 | synchronized (mVolumes) { |
| 706 | mStartedUsers = ArrayUtils.removeInt(mStartedUsers, userId); |
| 707 | } |
| 708 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 709 | |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 710 | void runIdleMaintenance(Runnable callback) { |
| 711 | mHandler.sendMessage(mHandler.obtainMessage(H_FSTRIM, callback)); |
| 712 | } |
| 713 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 714 | // Binder entry point for kicking off an immediate fstrim |
| 715 | @Override |
| 716 | public void runMaintenance() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 717 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 718 | runIdleMaintenance(null); |
| 719 | } |
| 720 | |
| 721 | @Override |
| 722 | public long lastMaintenance() { |
| 723 | return mLastMaintenance; |
| 724 | } |
| 725 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 726 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 727 | * Callback from NativeDaemonConnector |
| 728 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 729 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 730 | public void onDaemonConnected() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 731 | mDaemonConnected = true; |
| 732 | mHandler.obtainMessage(H_DAEMON_CONNECTED).sendToTarget(); |
| 733 | } |
| 734 | |
| 735 | private void handleDaemonConnected() { |
| 736 | resetIfReadyAndConnected(); |
| 737 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 738 | /* |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 739 | * Now that we've done our initialization, release |
| 740 | * the hounds! |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 741 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 742 | mConnectedSignal.countDown(); |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 743 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 744 | // On an encrypted device we can't see system properties yet, so pull |
| 745 | // the system locale out of the mount service. |
| 746 | if ("".equals(SystemProperties.get("vold.encrypt_progress"))) { |
| 747 | copyLocaleFromMountService(); |
| 748 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 749 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 750 | // Let package manager load internal ASECs. |
| 751 | mPms.scanAvailableAsecs(); |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 752 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 753 | // Notify people waiting for ASECs to be scanned that it's done. |
| 754 | mAsecsScanned.countDown(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 755 | } |
| 756 | |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 757 | private void copyLocaleFromMountService() { |
| 758 | String systemLocale; |
| 759 | try { |
| 760 | systemLocale = getField(StorageManager.SYSTEM_LOCALE_KEY); |
| 761 | } catch (RemoteException e) { |
| 762 | return; |
| 763 | } |
| 764 | if (TextUtils.isEmpty(systemLocale)) { |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | Slog.d(TAG, "Got locale " + systemLocale + " from mount service"); |
| 769 | Locale locale = Locale.forLanguageTag(systemLocale); |
| 770 | Configuration config = new Configuration(); |
| 771 | config.setLocale(locale); |
| 772 | try { |
| 773 | ActivityManagerNative.getDefault().updateConfiguration(config); |
| 774 | } catch (RemoteException e) { |
| 775 | Slog.e(TAG, "Error setting system locale from mount service", e); |
| 776 | } |
Elliott Hughes | 9c33f28 | 2014-10-13 12:39:56 -0700 | [diff] [blame] | 777 | |
| 778 | // Temporary workaround for http://b/17945169. |
| 779 | Slog.d(TAG, "Setting system properties to " + systemLocale + " from mount service"); |
Narayan Kamath | d30dbb8 | 2015-01-15 14:48:15 +0000 | [diff] [blame] | 780 | SystemProperties.set("persist.sys.locale", locale.toLanguageTag()); |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 781 | } |
| 782 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 783 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 784 | * Callback from NativeDaemonConnector |
| 785 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 786 | @Override |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 787 | public boolean onCheckHoldWakeLock(int code) { |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Callback from NativeDaemonConnector |
| 793 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 794 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 795 | public boolean onEvent(int code, String raw, String[] cooked) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 796 | synchronized (mLock) { |
| 797 | return onEventLocked(code, raw, cooked); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 798 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 799 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 800 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 801 | private boolean onEventLocked(int code, String raw, String[] cooked) { |
| 802 | switch (code) { |
| 803 | case VoldResponseCode.DISK_CREATED: { |
| 804 | if (cooked.length != 3) break; |
| 805 | final String id = cooked[1]; |
| 806 | final int flags = Integer.parseInt(cooked[2]); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 807 | mDisks.put(id, new DiskInfo(id, flags)); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 808 | break; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 809 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 810 | case VoldResponseCode.DISK_SIZE_CHANGED: { |
| 811 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 812 | final DiskInfo disk = mDisks.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 813 | if (disk != null) { |
| 814 | disk.size = Long.parseLong(cooked[2]); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 815 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 816 | break; |
| 817 | } |
| 818 | case VoldResponseCode.DISK_LABEL_CHANGED: { |
| 819 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 820 | final DiskInfo disk = mDisks.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 821 | if (disk != null) { |
| 822 | disk.label = cooked[2]; |
| 823 | } |
| 824 | break; |
| 825 | } |
| 826 | case VoldResponseCode.DISK_VOLUME_CREATED: { |
| 827 | if (cooked.length != 3) break; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 828 | final String diskId = cooked[1]; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 829 | final String volId = cooked[2]; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 830 | final DiskInfo disk = mDisks.get(diskId); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 831 | if (disk != null) { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 832 | disk.volumeIds = ArrayUtils.appendElement(String.class, disk.volumeIds, volId); |
| 833 | } |
| 834 | final VolumeInfo vol = mVolumes.get(volId); |
| 835 | if (vol != null) { |
| 836 | vol.diskId = diskId; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 837 | } |
| 838 | break; |
| 839 | } |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 840 | case VoldResponseCode.DISK_VOLUME_DESTROYED: { |
| 841 | if (cooked.length != 3) break; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 842 | final String diskId = cooked[1]; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 843 | final String volId = cooked[2]; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 844 | final DiskInfo disk = mDisks.get(diskId); |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 845 | if (disk != null) { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 846 | disk.volumeIds = ArrayUtils.removeElement(String.class, disk.volumeIds, volId); |
| 847 | } |
| 848 | final VolumeInfo vol = mVolumes.get(volId); |
| 849 | if (vol != null) { |
| 850 | vol.diskId = null; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 851 | } |
| 852 | break; |
| 853 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 854 | case VoldResponseCode.DISK_DESTROYED: { |
| 855 | if (cooked.length != 2) break; |
| 856 | mDisks.remove(cooked[1]); |
| 857 | break; |
| 858 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 859 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 860 | case VoldResponseCode.VOLUME_CREATED: { |
| 861 | if (cooked.length != 3) break; |
| 862 | final String id = cooked[1]; |
| 863 | final int type = Integer.parseInt(cooked[2]); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 864 | final int mtpIndex = allocateMtpIndex(id); |
| 865 | final VolumeInfo vol = new VolumeInfo(id, type, mtpIndex); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 866 | mVolumes.put(id, vol); |
| 867 | onVolumeCreatedLocked(vol); |
| 868 | break; |
| 869 | } |
| 870 | case VoldResponseCode.VOLUME_STATE_CHANGED: { |
| 871 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 872 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 873 | if (vol != null) { |
| 874 | final int oldState = vol.state; |
| 875 | final int newState = Integer.parseInt(cooked[2]); |
| 876 | vol.state = newState; |
| 877 | onVolumeStateChangedLocked(vol, oldState, newState); |
| 878 | } |
| 879 | break; |
| 880 | } |
| 881 | case VoldResponseCode.VOLUME_FS_TYPE_CHANGED: { |
| 882 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 883 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 884 | if (vol != null) { |
| 885 | vol.fsType = cooked[2]; |
| 886 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 887 | mCallbacks.notifyVolumeMetadataChanged(vol.clone()); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 888 | break; |
| 889 | } |
| 890 | case VoldResponseCode.VOLUME_FS_UUID_CHANGED: { |
| 891 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 892 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 893 | if (vol != null) { |
| 894 | vol.fsUuid = cooked[2]; |
| 895 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 896 | refreshMetadataLocked(); |
| 897 | mCallbacks.notifyVolumeMetadataChanged(vol.clone()); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 898 | break; |
| 899 | } |
| 900 | case VoldResponseCode.VOLUME_FS_LABEL_CHANGED: { |
| 901 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 902 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 903 | if (vol != null) { |
| 904 | vol.fsLabel = cooked[2]; |
| 905 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 906 | mCallbacks.notifyVolumeMetadataChanged(vol.clone()); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 907 | break; |
| 908 | } |
| 909 | case VoldResponseCode.VOLUME_PATH_CHANGED: { |
| 910 | if (cooked.length != 3) break; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 911 | final VolumeInfo vol = mVolumes.get(cooked[1]); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 912 | if (vol != null) { |
| 913 | vol.path = cooked[2]; |
| 914 | } |
| 915 | break; |
| 916 | } |
| 917 | case VoldResponseCode.VOLUME_DESTROYED: { |
| 918 | if (cooked.length != 2) break; |
| 919 | mVolumes.remove(cooked[1]); |
| 920 | break; |
| 921 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 922 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 923 | case VoldResponseCode.FstrimCompleted: { |
Svetoslav | 9e814a8 | 2013-04-30 10:43:56 -0700 | [diff] [blame] | 924 | EventLogTags.writeFstrimFinish(SystemClock.elapsedRealtime()); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 925 | break; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 926 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 927 | default: { |
| 928 | Slog.d(TAG, "Unhandled vold event " + code); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 929 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 932 | return true; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 935 | private void onVolumeCreatedLocked(VolumeInfo vol) { |
| 936 | final boolean primaryPhysical = SystemProperties.getBoolean( |
| 937 | StorageManager.PROP_PRIMARY_PHYSICAL, false); |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 938 | // TODO: enable switching to another emulated primary |
| 939 | if (VolumeInfo.ID_EMULATED_INTERNAL.equals(vol.id) && !primaryPhysical) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 940 | vol.flags |= VolumeInfo.FLAG_PRIMARY; |
| 941 | vol.flags |= VolumeInfo.FLAG_VISIBLE; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 942 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 943 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 944 | } else if (vol.type == VolumeInfo.TYPE_PUBLIC) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 945 | if (primaryPhysical) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 946 | vol.flags |= VolumeInfo.FLAG_PRIMARY; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 947 | } |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 948 | vol.flags |= VolumeInfo.FLAG_VISIBLE; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 949 | vol.userId = UserHandle.USER_OWNER; |
| 950 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 951 | |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 952 | } else if (vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 953 | mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget(); |
| 954 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 955 | } else { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 956 | Slog.d(TAG, "Skipping automatic mounting of " + vol); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 960 | private void onVolumeStateChangedLocked(VolumeInfo vol, int oldState, int newState) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 961 | mCallbacks.notifyVolumeStateChanged(vol.clone(), oldState, newState); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 962 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 963 | final String oldStateEnv = VolumeInfo.getEnvironmentForState(oldState); |
| 964 | final String newStateEnv = VolumeInfo.getEnvironmentForState(newState); |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 965 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 966 | if (!Objects.equals(oldStateEnv, newStateEnv)) { |
| 967 | // Kick state changed event towards all started users. Any users |
| 968 | // started after this point will trigger additional |
| 969 | // user-specific broadcasts. |
| 970 | for (int userId : mStartedUsers) { |
| 971 | if (vol.isVisibleToUser(userId)) { |
| 972 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId); |
| 973 | mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 974 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 975 | mCallbacks.notifyStorageStateChanged(userVol.getPath(), oldStateEnv, |
| 976 | newStateEnv); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 980 | |
| 981 | if (vol.type == VolumeInfo.TYPE_PUBLIC && vol.state == VolumeInfo.STATE_UNMOUNTING) { |
| 982 | // TODO: this should eventually be handled by new ObbVolume state changes |
| 983 | /* |
| 984 | * Some OBBs might have been unmounted when this volume was |
| 985 | * unmounted, so send a message to the handler to let it know to |
| 986 | * remove those from the list of mounted OBBS. |
| 987 | */ |
| 988 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage( |
| 989 | OBB_FLUSH_MOUNT_STATE, vol.path)); |
| 990 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 993 | /** |
| 994 | * Refresh latest metadata into any currently active {@link VolumeInfo}. |
| 995 | */ |
| 996 | private void refreshMetadataLocked() { |
| 997 | final int size = mVolumes.size(); |
| 998 | for (int i = 0; i < size; i++) { |
| 999 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 1000 | final VolumeMetadata meta = mMetadata.get(vol.fsUuid); |
| 1001 | |
| 1002 | if (meta != null) { |
| 1003 | vol.nickname = meta.nickname; |
| 1004 | vol.userFlags = meta.userFlags; |
| 1005 | } else { |
| 1006 | vol.nickname = null; |
| 1007 | vol.userFlags = 0; |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1012 | private void enforcePermission(String perm) { |
| 1013 | mContext.enforceCallingOrSelfPermission(perm, perm); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 1014 | } |
| 1015 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1016 | private void enforceUserRestriction(String restriction) { |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 1017 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1018 | if (um.hasUserRestriction(restriction, Binder.getCallingUserHandle())) { |
Emily Bernier | 92aa5a2 | 2014-07-07 10:11:48 -0400 | [diff] [blame] | 1019 | throw new SecurityException("User has restriction " + restriction); |
| 1020 | } |
| 1021 | } |
| 1022 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1023 | /** |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1024 | * Constructs a new MountService instance |
| 1025 | * |
| 1026 | * @param context Binder context for this service |
| 1027 | */ |
| 1028 | public MountService(Context context) { |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 1029 | sSelf = this; |
| 1030 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1031 | mContext = context; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1032 | mCallbacks = new Callbacks(FgThread.get().getLooper()); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1033 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1034 | // XXX: This will go away soon in favor of IMountServiceObserver |
| 1035 | mPms = (PackageManagerService) ServiceManager.getService("package"); |
| 1036 | |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 1037 | HandlerThread hthread = new HandlerThread(TAG); |
| 1038 | hthread.start(); |
| 1039 | mHandler = new MountServiceHandler(hthread.getLooper()); |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 1040 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1041 | // Add OBB Action Handler to MountService thread. |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 1042 | mObbActionHandler = new ObbActionHandler(IoThread.get().getLooper()); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1043 | |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 1044 | // Initialize the last-fstrim tracking if necessary |
| 1045 | File dataDir = Environment.getDataDirectory(); |
| 1046 | File systemDir = new File(dataDir, "system"); |
| 1047 | mLastMaintenanceFile = new File(systemDir, LAST_FSTRIM_FILE); |
| 1048 | if (!mLastMaintenanceFile.exists()) { |
| 1049 | // Not setting mLastMaintenance here means that we will force an |
| 1050 | // fstrim during reboot following the OTA that installs this code. |
| 1051 | try { |
| 1052 | (new FileOutputStream(mLastMaintenanceFile)).close(); |
| 1053 | } catch (IOException e) { |
| 1054 | Slog.e(TAG, "Unable to create fstrim record " + mLastMaintenanceFile.getPath()); |
| 1055 | } |
| 1056 | } else { |
| 1057 | mLastMaintenance = mLastMaintenanceFile.lastModified(); |
| 1058 | } |
| 1059 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1060 | mMetadataFile = new AtomicFile( |
| 1061 | new File(Environment.getSystemSecureDirectory(), "storage.xml")); |
| 1062 | |
| 1063 | synchronized (mLock) { |
| 1064 | readMetadataLocked(); |
| 1065 | } |
| 1066 | |
Marco Nelissen | c34ebce | 2010-02-18 13:39:41 -0800 | [diff] [blame] | 1067 | /* |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 1068 | * Create the connection to vold with a maximum queue of twice the |
| 1069 | * amount of containers we'd ever expect to have. This keeps an |
| 1070 | * "asec list" from blocking a thread repeatedly. |
| 1071 | */ |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1072 | mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG, 25, |
| 1073 | null); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1074 | mConnector.setDebug(true); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 1075 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 1076 | Thread thread = new Thread(mConnector, VOLD_TAG); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1077 | thread.start(); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 1078 | |
Kenny Root | 07714d4 | 2011-08-17 17:49:28 -0700 | [diff] [blame] | 1079 | // Add ourself to the Watchdog monitors if enabled. |
| 1080 | if (WATCHDOG_ENABLE) { |
| 1081 | Watchdog.getInstance().addMonitor(this); |
| 1082 | } |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Jeff Sharkey | 56e6293 | 2015-03-21 20:41:00 -0700 | [diff] [blame] | 1085 | private void systemReady() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1086 | mSystemReady = true; |
| 1087 | mHandler.obtainMessage(H_SYSTEM_READY).sendToTarget(); |
| 1088 | } |
| 1089 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1090 | private void readMetadataLocked() { |
| 1091 | mMetadata.clear(); |
| 1092 | |
| 1093 | FileInputStream fis = null; |
| 1094 | try { |
| 1095 | fis = mMetadataFile.openRead(); |
| 1096 | final XmlPullParser in = Xml.newPullParser(); |
| 1097 | in.setInput(fis, null); |
| 1098 | |
| 1099 | int type; |
| 1100 | while ((type = in.next()) != END_DOCUMENT) { |
| 1101 | if (type == START_TAG) { |
| 1102 | final String tag = in.getName(); |
| 1103 | if (TAG_VOLUME.equals(tag)) { |
| 1104 | final VolumeMetadata meta = VolumeMetadata.read(in); |
| 1105 | mMetadata.put(meta.fsUuid, meta); |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | } catch (FileNotFoundException e) { |
| 1110 | // Missing metadata is okay, probably first boot |
| 1111 | } catch (IOException e) { |
| 1112 | Slog.wtf(TAG, "Failed reading metadata", e); |
| 1113 | } catch (XmlPullParserException e) { |
| 1114 | Slog.wtf(TAG, "Failed reading metadata", e); |
| 1115 | } finally { |
| 1116 | IoUtils.closeQuietly(fis); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | private void writeMetadataLocked() { |
| 1121 | FileOutputStream fos = null; |
| 1122 | try { |
| 1123 | fos = mMetadataFile.startWrite(); |
| 1124 | |
| 1125 | XmlSerializer out = new FastXmlSerializer(); |
| 1126 | out.setOutput(fos, "utf-8"); |
| 1127 | out.startDocument(null, true); |
| 1128 | out.startTag(null, TAG_VOLUMES); |
| 1129 | final int size = mMetadata.size(); |
| 1130 | for (int i = 0; i < size; i++) { |
| 1131 | final VolumeMetadata meta = mMetadata.valueAt(i); |
| 1132 | VolumeMetadata.write(out, meta); |
| 1133 | } |
| 1134 | out.endTag(null, TAG_VOLUMES); |
| 1135 | out.endDocument(); |
| 1136 | |
| 1137 | mMetadataFile.finishWrite(fos); |
| 1138 | } catch (IOException e) { |
| 1139 | if (fos != null) { |
| 1140 | mMetadataFile.failWrite(fos); |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1145 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1146 | * Exposed API calls below here |
| 1147 | */ |
| 1148 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1149 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1150 | public void registerListener(IMountServiceListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1151 | mCallbacks.register(listener); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1154 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1155 | public void unregisterListener(IMountServiceListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1156 | mCallbacks.unregister(listener); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1159 | @Override |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 1160 | public void shutdown(final IMountShutdownObserver observer) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1161 | enforcePermission(android.Manifest.permission.SHUTDOWN); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1162 | |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1163 | Slog.i(TAG, "Shutting down"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1164 | mHandler.obtainMessage(H_SHUTDOWN, observer).sendToTarget(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1167 | @Override |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1168 | public boolean isUsbMassStorageConnected() { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1169 | throw new UnsupportedOperationException(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1172 | @Override |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1173 | public void setUsbMassStorageEnabled(boolean enable) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1174 | throw new UnsupportedOperationException(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1177 | @Override |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1178 | public boolean isUsbMassStorageEnabled() { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1179 | throw new UnsupportedOperationException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | } |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1181 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1182 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1183 | public String getVolumeState(String mountPoint) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1184 | throw new UnsupportedOperationException(); |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1187 | @Override |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1188 | public boolean isExternalStorageEmulated() { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1189 | throw new UnsupportedOperationException(); |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1192 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1193 | public int mountVolume(String path) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1194 | mount(findVolumeIdForPath(path)); |
| 1195 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1198 | @Override |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 1199 | public void unmountVolume(String path, boolean force, boolean removeEncryption) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1200 | unmount(findVolumeIdForPath(path)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1203 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1204 | public int formatVolume(String path) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1205 | format(findVolumeIdForPath(path)); |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | @Override |
| 1210 | public void mount(String volId) { |
| 1211 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1212 | waitForReady(); |
| 1213 | |
| 1214 | final VolumeInfo vol = findVolumeById(volId); |
| 1215 | if (vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_PRIVATE) { |
| 1216 | enforceUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA); |
| 1217 | } |
| 1218 | try { |
| 1219 | mConnector.execute("volume", "mount", vol.id, vol.flags, vol.userId); |
| 1220 | } catch (NativeDaemonConnectorException e) { |
| 1221 | throw e.rethrowAsParcelableException(); |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | @Override |
| 1226 | public void unmount(String volId) { |
| 1227 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1228 | waitForReady(); |
| 1229 | |
| 1230 | final VolumeInfo vol = findVolumeById(volId); |
| 1231 | |
| 1232 | // TODO: expand PMS to know about multiple volumes |
| 1233 | if (vol.isPrimary()) { |
| 1234 | synchronized (mUnmountLock) { |
| 1235 | mUnmountSignal = new CountDownLatch(1); |
| 1236 | mPms.updateExternalMediaStatus(false, true); |
| 1237 | waitForLatch(mUnmountSignal, "mUnmountSignal"); |
| 1238 | mUnmountSignal = null; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | try { |
| 1243 | mConnector.execute("volume", "unmount", vol.id); |
| 1244 | } catch (NativeDaemonConnectorException e) { |
| 1245 | throw e.rethrowAsParcelableException(); |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | @Override |
| 1250 | public void format(String volId) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1251 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1252 | waitForReady(); |
San Mehat | 5b77dab | 2010-01-26 13:28:50 -0800 | [diff] [blame] | 1253 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1254 | final VolumeInfo vol = findVolumeById(volId); |
| 1255 | try { |
| 1256 | mConnector.execute("volume", "format", vol.id); |
| 1257 | } catch (NativeDaemonConnectorException e) { |
| 1258 | throw e.rethrowAsParcelableException(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1259 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | @Override |
| 1263 | public void partitionPublic(String diskId) { |
| 1264 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| 1265 | waitForReady(); |
| 1266 | |
| 1267 | try { |
| 1268 | mConnector.execute("volume", "partition", diskId, "public"); |
| 1269 | } catch (NativeDaemonConnectorException e) { |
| 1270 | throw e.rethrowAsParcelableException(); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | @Override |
| 1275 | public void partitionPrivate(String diskId) { |
| 1276 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| 1277 | waitForReady(); |
| 1278 | |
| 1279 | try { |
| 1280 | mConnector.execute("volume", "partition", diskId, "private"); |
| 1281 | } catch (NativeDaemonConnectorException e) { |
| 1282 | throw e.rethrowAsParcelableException(); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | @Override |
| 1287 | public void partitionMixed(String diskId, int ratio) { |
| 1288 | enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| 1289 | waitForReady(); |
| 1290 | |
| 1291 | try { |
| 1292 | mConnector.execute("volume", "partition", diskId, "mixed", ratio); |
| 1293 | } catch (NativeDaemonConnectorException e) { |
| 1294 | throw e.rethrowAsParcelableException(); |
| 1295 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1298 | @Override |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 1299 | public void setVolumeNickname(String volId, String nickname) { |
| 1300 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1301 | waitForReady(); |
| 1302 | |
| 1303 | synchronized (mLock) { |
| 1304 | final VolumeInfo vol = findVolumeById(volId); |
| 1305 | final VolumeMetadata meta = findOrCreateMetadataLocked(vol); |
| 1306 | meta.nickname = nickname; |
| 1307 | refreshMetadataLocked(); |
| 1308 | writeMetadataLocked(); |
| 1309 | mCallbacks.notifyVolumeMetadataChanged(vol.clone()); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | @Override |
| 1314 | public void setVolumeUserFlags(String volId, int flags, int mask) { |
| 1315 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1316 | waitForReady(); |
| 1317 | |
| 1318 | synchronized (mLock) { |
| 1319 | final VolumeInfo vol = findVolumeById(volId); |
| 1320 | final VolumeMetadata meta = findOrCreateMetadataLocked(vol); |
| 1321 | meta.userFlags = (meta.userFlags & ~mask) | (flags & mask); |
| 1322 | refreshMetadataLocked(); |
| 1323 | writeMetadataLocked(); |
| 1324 | mCallbacks.notifyVolumeMetadataChanged(vol.clone()); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | @Override |
Mike Lockwood | ecedfdc | 2011-06-08 15:11:59 -0700 | [diff] [blame] | 1329 | public int[] getStorageUsers(String path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1330 | enforcePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1331 | waitForReady(); |
| 1332 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1333 | final String[] r = NativeDaemonEvent.filterMessageList( |
| 1334 | mConnector.executeForList("storage", "users", path), |
| 1335 | VoldResponseCode.StorageUsersListResult); |
| 1336 | |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1337 | // FMT: <pid> <process name> |
| 1338 | int[] data = new int[r.length]; |
| 1339 | for (int i = 0; i < r.length; i++) { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1340 | String[] tok = r[i].split(" "); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1341 | try { |
| 1342 | data[i] = Integer.parseInt(tok[0]); |
| 1343 | } catch (NumberFormatException nfe) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1344 | Slog.e(TAG, String.format("Error parsing pid %s", tok[0])); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1345 | return new int[0]; |
| 1346 | } |
| 1347 | } |
| 1348 | return data; |
| 1349 | } catch (NativeDaemonConnectorException e) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1350 | Slog.e(TAG, "Failed to retrieve storage users list", e); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1351 | return new int[0]; |
| 1352 | } |
| 1353 | } |
| 1354 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1355 | private void warnOnNotMounted() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1356 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1357 | for (int i = 0; i < mVolumes.size(); i++) { |
| 1358 | final VolumeInfo vol = mVolumes.valueAt(i); |
| 1359 | if (vol.isPrimary() && vol.state == VolumeInfo.STATE_MOUNTED) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1360 | // Cool beans, we have a mounted primary volume |
| 1361 | return; |
| 1362 | } |
Jeff Sharkey | 32ee831 | 2012-09-30 13:21:31 -0700 | [diff] [blame] | 1363 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1364 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1365 | |
| 1366 | Slog.w(TAG, "No primary storage mounted!"); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1369 | public String[] getSecureContainerList() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1370 | enforcePermission(android.Manifest.permission.ASEC_ACCESS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1371 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1372 | warnOnNotMounted(); |
San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1373 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1374 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1375 | return NativeDaemonEvent.filterMessageList( |
| 1376 | mConnector.executeForList("asec", "list"), VoldResponseCode.AsecListResult); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1377 | } catch (NativeDaemonConnectorException e) { |
| 1378 | return new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1379 | } |
| 1380 | } |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1381 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 1382 | public int createSecureContainer(String id, int sizeMb, String fstype, String key, |
| 1383 | int ownerUid, boolean external) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1384 | enforcePermission(android.Manifest.permission.ASEC_CREATE); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1385 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1386 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1387 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1388 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1389 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 1390 | mConnector.execute("asec", "create", id, sizeMb, fstype, new SensitiveArg(key), |
| 1391 | ownerUid, external ? "1" : "0"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1392 | } catch (NativeDaemonConnectorException e) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1393 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1394 | } |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1395 | |
| 1396 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1397 | synchronized (mAsecMountSet) { |
| 1398 | mAsecMountSet.add(id); |
| 1399 | } |
| 1400 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1401 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1402 | } |
| 1403 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1404 | @Override |
| 1405 | public int resizeSecureContainer(String id, int sizeMb, String key) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1406 | enforcePermission(android.Manifest.permission.ASEC_CREATE); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1407 | waitForReady(); |
| 1408 | warnOnNotMounted(); |
| 1409 | |
| 1410 | int rc = StorageResultCode.OperationSucceeded; |
| 1411 | try { |
| 1412 | mConnector.execute("asec", "resize", id, sizeMb, new SensitiveArg(key)); |
| 1413 | } catch (NativeDaemonConnectorException e) { |
| 1414 | rc = StorageResultCode.OperationFailedInternalError; |
| 1415 | } |
| 1416 | return rc; |
| 1417 | } |
| 1418 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1419 | public int finalizeSecureContainer(String id) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1420 | enforcePermission(android.Manifest.permission.ASEC_CREATE); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1421 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1422 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1423 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1424 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1425 | mConnector.execute("asec", "finalize", id); |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1426 | /* |
| 1427 | * Finalization does a remount, so no need |
| 1428 | * to update mAsecMountSet |
| 1429 | */ |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1430 | } catch (NativeDaemonConnectorException e) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1431 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1432 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1433 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 1436 | public int fixPermissionsSecureContainer(String id, int gid, String filename) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1437 | enforcePermission(android.Manifest.permission.ASEC_CREATE); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 1438 | warnOnNotMounted(); |
| 1439 | |
| 1440 | int rc = StorageResultCode.OperationSucceeded; |
| 1441 | try { |
| 1442 | mConnector.execute("asec", "fixperms", id, gid, filename); |
| 1443 | /* |
| 1444 | * Fix permissions does a remount, so no need to update |
| 1445 | * mAsecMountSet |
| 1446 | */ |
| 1447 | } catch (NativeDaemonConnectorException e) { |
| 1448 | rc = StorageResultCode.OperationFailedInternalError; |
| 1449 | } |
| 1450 | return rc; |
| 1451 | } |
| 1452 | |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1453 | public int destroySecureContainer(String id, boolean force) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1454 | enforcePermission(android.Manifest.permission.ASEC_DESTROY); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1455 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1456 | warnOnNotMounted(); |
San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1457 | |
Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 1458 | /* |
| 1459 | * Force a GC to make sure AssetManagers in other threads of the |
| 1460 | * system_server are cleaned up. We have to do this since AssetManager |
| 1461 | * instances are kept as a WeakReference and it's possible we have files |
| 1462 | * open on the external storage. |
| 1463 | */ |
| 1464 | Runtime.getRuntime().gc(); |
| 1465 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1466 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1467 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1468 | final Command cmd = new Command("asec", "destroy", id); |
| 1469 | if (force) { |
| 1470 | cmd.appendArg("force"); |
| 1471 | } |
| 1472 | mConnector.execute(cmd); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1473 | } catch (NativeDaemonConnectorException e) { |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1474 | int code = e.getCode(); |
| 1475 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1476 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 1477 | } else { |
| 1478 | rc = StorageResultCode.OperationFailedInternalError; |
| 1479 | } |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1480 | } |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1481 | |
| 1482 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1483 | synchronized (mAsecMountSet) { |
| 1484 | if (mAsecMountSet.contains(id)) { |
| 1485 | mAsecMountSet.remove(id); |
| 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1490 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1491 | } |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1492 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1493 | public int mountSecureContainer(String id, String key, int ownerUid, boolean readOnly) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1494 | enforcePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1495 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1496 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1497 | |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1498 | synchronized (mAsecMountSet) { |
| 1499 | if (mAsecMountSet.contains(id)) { |
| 1500 | return StorageResultCode.OperationFailedStorageMounted; |
| 1501 | } |
| 1502 | } |
| 1503 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1504 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1505 | try { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1506 | mConnector.execute("asec", "mount", id, new SensitiveArg(key), ownerUid, |
| 1507 | readOnly ? "ro" : "rw"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1508 | } catch (NativeDaemonConnectorException e) { |
Kenny Root | f030462 | 2010-03-19 19:20:42 -0700 | [diff] [blame] | 1509 | int code = e.getCode(); |
| 1510 | if (code != VoldResponseCode.OpFailedStorageBusy) { |
| 1511 | rc = StorageResultCode.OperationFailedInternalError; |
| 1512 | } |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1513 | } |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1514 | |
| 1515 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1516 | synchronized (mAsecMountSet) { |
| 1517 | mAsecMountSet.add(id); |
| 1518 | } |
| 1519 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1520 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1523 | public int unmountSecureContainer(String id, boolean force) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1524 | enforcePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1525 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1526 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1527 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1528 | synchronized (mAsecMountSet) { |
| 1529 | if (!mAsecMountSet.contains(id)) { |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1530 | return StorageResultCode.OperationFailedStorageNotMounted; |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 1534 | /* |
| 1535 | * Force a GC to make sure AssetManagers in other threads of the |
| 1536 | * system_server are cleaned up. We have to do this since AssetManager |
| 1537 | * instances are kept as a WeakReference and it's possible we have files |
| 1538 | * open on the external storage. |
| 1539 | */ |
| 1540 | Runtime.getRuntime().gc(); |
| 1541 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1542 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1543 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1544 | final Command cmd = new Command("asec", "unmount", id); |
| 1545 | if (force) { |
| 1546 | cmd.appendArg("force"); |
| 1547 | } |
| 1548 | mConnector.execute(cmd); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1549 | } catch (NativeDaemonConnectorException e) { |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1550 | int code = e.getCode(); |
| 1551 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1552 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 1553 | } else { |
| 1554 | rc = StorageResultCode.OperationFailedInternalError; |
| 1555 | } |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1556 | } |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1557 | |
| 1558 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1559 | synchronized (mAsecMountSet) { |
| 1560 | mAsecMountSet.remove(id); |
| 1561 | } |
| 1562 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1563 | return rc; |
San Mehat | 9dba709 | 2010-01-18 06:47:41 -0800 | [diff] [blame] | 1564 | } |
| 1565 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1566 | public boolean isSecureContainerMounted(String id) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1567 | enforcePermission(android.Manifest.permission.ASEC_ACCESS); |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1568 | waitForReady(); |
| 1569 | warnOnNotMounted(); |
| 1570 | |
| 1571 | synchronized (mAsecMountSet) { |
| 1572 | return mAsecMountSet.contains(id); |
| 1573 | } |
| 1574 | } |
| 1575 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1576 | public int renameSecureContainer(String oldId, String newId) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1577 | enforcePermission(android.Manifest.permission.ASEC_RENAME); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1578 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1579 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1580 | |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1581 | synchronized (mAsecMountSet) { |
San Mehat | 85451ee | 2010-02-24 08:54:18 -0800 | [diff] [blame] | 1582 | /* |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1583 | * Because a mounted container has active internal state which cannot be |
San Mehat | 85451ee | 2010-02-24 08:54:18 -0800 | [diff] [blame] | 1584 | * changed while active, we must ensure both ids are not currently mounted. |
| 1585 | */ |
| 1586 | if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) { |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1587 | return StorageResultCode.OperationFailedStorageMounted; |
| 1588 | } |
| 1589 | } |
| 1590 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1591 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1592 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1593 | mConnector.execute("asec", "rename", oldId, newId); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1594 | } catch (NativeDaemonConnectorException e) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1595 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1596 | } |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1597 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1598 | return rc; |
San Mehat | 45f6104 | 2010-01-23 08:12:43 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1601 | public String getSecureContainerPath(String id) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1602 | enforcePermission(android.Manifest.permission.ASEC_ACCESS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1603 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1604 | warnOnNotMounted(); |
San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1605 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1606 | final NativeDaemonEvent event; |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1607 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1608 | event = mConnector.execute("asec", "path", id); |
| 1609 | event.checkCode(VoldResponseCode.AsecPathResult); |
| 1610 | return event.getMessage(); |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1611 | } catch (NativeDaemonConnectorException e) { |
| 1612 | int code = e.getCode(); |
| 1613 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
Fredrik Helmer | a20c8ef | 2011-02-09 16:16:10 +0100 | [diff] [blame] | 1614 | Slog.i(TAG, String.format("Container '%s' not found", id)); |
| 1615 | return null; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1616 | } else { |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1617 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1618 | } |
| 1619 | } |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1620 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 1621 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 1622 | public String getSecureContainerFilesystemPath(String id) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1623 | enforcePermission(android.Manifest.permission.ASEC_ACCESS); |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 1624 | waitForReady(); |
| 1625 | warnOnNotMounted(); |
| 1626 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1627 | final NativeDaemonEvent event; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 1628 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1629 | event = mConnector.execute("asec", "fspath", id); |
| 1630 | event.checkCode(VoldResponseCode.AsecPathResult); |
| 1631 | return event.getMessage(); |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 1632 | } catch (NativeDaemonConnectorException e) { |
| 1633 | int code = e.getCode(); |
| 1634 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
| 1635 | Slog.i(TAG, String.format("Container '%s' not found", id)); |
| 1636 | return null; |
| 1637 | } else { |
| 1638 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1643 | @Override |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 1644 | public void finishMediaUpdate() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1645 | if (mUnmountSignal != null) { |
| 1646 | mUnmountSignal.countDown(); |
| 1647 | } else { |
| 1648 | Slog.w(TAG, "Odd, nobody asked to unmount?"); |
| 1649 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 1650 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1651 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1652 | private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) { |
| 1653 | if (callerUid == android.os.Process.SYSTEM_UID) { |
| 1654 | return true; |
| 1655 | } |
| 1656 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1657 | if (packageName == null) { |
| 1658 | return false; |
| 1659 | } |
| 1660 | |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1661 | final int packageUid = mPms.getPackageUid(packageName, UserHandle.getUserId(callerUid)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1662 | |
| 1663 | if (DEBUG_OBB) { |
| 1664 | Slog.d(TAG, "packageName = " + packageName + ", packageUid = " + |
| 1665 | packageUid + ", callerUid = " + callerUid); |
| 1666 | } |
| 1667 | |
| 1668 | return callerUid == packageUid; |
| 1669 | } |
| 1670 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1671 | public String getMountedObbPath(String rawPath) { |
| 1672 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 1673 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1674 | waitForReady(); |
| 1675 | warnOnNotMounted(); |
| 1676 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1677 | final ObbState state; |
| 1678 | synchronized (mObbPathToStateMap) { |
| 1679 | state = mObbPathToStateMap.get(rawPath); |
| 1680 | } |
| 1681 | if (state == null) { |
| 1682 | Slog.w(TAG, "Failed to find OBB mounted at " + rawPath); |
| 1683 | return null; |
| 1684 | } |
| 1685 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1686 | final NativeDaemonEvent event; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1687 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1688 | event = mConnector.execute("obb", "path", state.voldPath); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1689 | event.checkCode(VoldResponseCode.AsecPathResult); |
| 1690 | return event.getMessage(); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1691 | } catch (NativeDaemonConnectorException e) { |
| 1692 | int code = e.getCode(); |
| 1693 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1694 | return null; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1695 | } else { |
| 1696 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1701 | @Override |
| 1702 | public boolean isObbMounted(String rawPath) { |
| 1703 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 1704 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1705 | return mObbPathToStateMap.containsKey(rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 1706 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1709 | @Override |
| 1710 | public void mountObb( |
| 1711 | String rawPath, String canonicalPath, String key, IObbActionListener token, int nonce) { |
| 1712 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
| 1713 | Preconditions.checkNotNull(canonicalPath, "canonicalPath cannot be null"); |
| 1714 | Preconditions.checkNotNull(token, "token cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 1715 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1716 | final int callingUid = Binder.getCallingUid(); |
| 1717 | final ObbState obbState = new ObbState(rawPath, canonicalPath, callingUid, token, nonce); |
| 1718 | final ObbAction action = new MountObbAction(obbState, key, callingUid); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1719 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
| 1720 | |
| 1721 | if (DEBUG_OBB) |
| 1722 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1725 | @Override |
| 1726 | public void unmountObb(String rawPath, boolean force, IObbActionListener token, int nonce) { |
| 1727 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
| 1728 | |
| 1729 | final ObbState existingState; |
| 1730 | synchronized (mObbPathToStateMap) { |
| 1731 | existingState = mObbPathToStateMap.get(rawPath); |
Kenny Root | f1121dc | 2010-09-29 07:30:53 -0700 | [diff] [blame] | 1732 | } |
| 1733 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1734 | if (existingState != null) { |
| 1735 | // TODO: separate state object from request data |
| 1736 | final int callingUid = Binder.getCallingUid(); |
| 1737 | final ObbState newState = new ObbState( |
| 1738 | rawPath, existingState.canonicalPath, callingUid, token, nonce); |
| 1739 | final ObbAction action = new UnmountObbAction(newState, force); |
| 1740 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1741 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1742 | if (DEBUG_OBB) |
| 1743 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
| 1744 | } else { |
| 1745 | Slog.w(TAG, "Unknown OBB mount at " + rawPath); |
| 1746 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 1749 | @Override |
| 1750 | public int getEncryptionState() { |
| 1751 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 1752 | "no permission to access the crypt keeper"); |
| 1753 | |
| 1754 | waitForReady(); |
| 1755 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1756 | final NativeDaemonEvent event; |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 1757 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1758 | event = mConnector.execute("cryptfs", "cryptocomplete"); |
| 1759 | return Integer.parseInt(event.getMessage()); |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 1760 | } catch (NumberFormatException e) { |
| 1761 | // Bad result - unexpected. |
| 1762 | Slog.w(TAG, "Unable to parse result from cryptfs cryptocomplete"); |
| 1763 | return ENCRYPTION_STATE_ERROR_UNKNOWN; |
| 1764 | } catch (NativeDaemonConnectorException e) { |
| 1765 | // Something bad happened. |
| 1766 | Slog.w(TAG, "Error in communicating with cryptfs in validating"); |
| 1767 | return ENCRYPTION_STATE_ERROR_UNKNOWN; |
| 1768 | } |
| 1769 | } |
| 1770 | |
Narayan Kamath | 1653b1d | 2014-12-17 13:40:36 +0000 | [diff] [blame] | 1771 | private static String toHex(String password) { |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 1772 | if (password == null) { |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1773 | return ""; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 1774 | } |
| 1775 | byte[] bytes = password.getBytes(StandardCharsets.UTF_8); |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1776 | return new String(HexEncoding.encode(bytes)); |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 1777 | } |
| 1778 | |
Narayan Kamath | 1653b1d | 2014-12-17 13:40:36 +0000 | [diff] [blame] | 1779 | private static String fromHex(String hexPassword) throws IllegalArgumentException { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1780 | if (hexPassword == null) { |
| 1781 | return null; |
| 1782 | } |
| 1783 | |
Narayan Kamath | 1653b1d | 2014-12-17 13:40:36 +0000 | [diff] [blame] | 1784 | final byte[] bytes = HexEncoding.decode(hexPassword.toCharArray(), false); |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1785 | return new String(bytes, StandardCharsets.UTF_8); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 1788 | @Override |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 1789 | public int decryptStorage(String password) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 1790 | if (TextUtils.isEmpty(password)) { |
| 1791 | throw new IllegalArgumentException("password cannot be empty"); |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 1792 | } |
| 1793 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 1794 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 1795 | "no permission to access the crypt keeper"); |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 1796 | |
| 1797 | waitForReady(); |
| 1798 | |
| 1799 | if (DEBUG_EVENTS) { |
| 1800 | Slog.i(TAG, "decrypting storage..."); |
| 1801 | } |
| 1802 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1803 | final NativeDaemonEvent event; |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 1804 | try { |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 1805 | event = mConnector.execute("cryptfs", "checkpw", new SensitiveArg(toHex(password))); |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1806 | |
Fredrik Roubert | da6aedf | 2011-12-20 17:34:43 +0100 | [diff] [blame] | 1807 | final int code = Integer.parseInt(event.getMessage()); |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1808 | if (code == 0) { |
| 1809 | // Decrypt was successful. Post a delayed message before restarting in order |
| 1810 | // to let the UI to clear itself |
| 1811 | mHandler.postDelayed(new Runnable() { |
| 1812 | public void run() { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 1813 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1814 | mConnector.execute("cryptfs", "restart"); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 1815 | } catch (NativeDaemonConnectorException e) { |
| 1816 | Slog.e(TAG, "problem executing in background", e); |
| 1817 | } |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1818 | } |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 1819 | }, 1000); // 1 second |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | return code; |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 1823 | } catch (NativeDaemonConnectorException e) { |
| 1824 | // Decryption failed |
| 1825 | return e.getCode(); |
| 1826 | } |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 1827 | } |
| 1828 | |
Paul Lawrence | 46791e7 | 2014-04-03 09:10:26 -0700 | [diff] [blame] | 1829 | public int encryptStorage(int type, String password) { |
| 1830 | if (TextUtils.isEmpty(password) && type != StorageManager.CRYPT_TYPE_DEFAULT) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 1831 | throw new IllegalArgumentException("password cannot be empty"); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 1832 | } |
| 1833 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 1834 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 1835 | "no permission to access the crypt keeper"); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 1836 | |
| 1837 | waitForReady(); |
| 1838 | |
| 1839 | if (DEBUG_EVENTS) { |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 1840 | Slog.i(TAG, "encrypting storage..."); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | try { |
Paul Lawrence | 46791e7 | 2014-04-03 09:10:26 -0700 | [diff] [blame] | 1844 | mConnector.execute("cryptfs", "enablecrypto", "inplace", CRYPTO_TYPES[type], |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 1845 | new SensitiveArg(toHex(password))); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 1846 | } catch (NativeDaemonConnectorException e) { |
| 1847 | // Encryption failed |
| 1848 | return e.getCode(); |
| 1849 | } |
| 1850 | |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 1854 | /** Set the password for encrypting the master key. |
| 1855 | * @param type One of the CRYPTO_TYPE_XXX consts defined in StorageManager. |
| 1856 | * @param password The password to set. |
| 1857 | */ |
| 1858 | public int changeEncryptionPassword(int type, String password) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 1859 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 1860 | "no permission to access the crypt keeper"); |
| 1861 | |
| 1862 | waitForReady(); |
| 1863 | |
| 1864 | if (DEBUG_EVENTS) { |
| 1865 | Slog.i(TAG, "changing encryption password..."); |
| 1866 | } |
| 1867 | |
| 1868 | try { |
Svetoslav | a6711ff | 2014-10-17 11:38:06 -0700 | [diff] [blame] | 1869 | NativeDaemonEvent event = mConnector.execute("cryptfs", "changepw", CRYPTO_TYPES[type], |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 1870 | new SensitiveArg(toHex(password))); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1871 | return Integer.parseInt(event.getMessage()); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 1872 | } catch (NativeDaemonConnectorException e) { |
| 1873 | // Encryption failed |
| 1874 | return e.getCode(); |
| 1875 | } |
| 1876 | } |
| 1877 | |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 1878 | /** |
| 1879 | * Validate a user-supplied password string with cryptfs |
| 1880 | */ |
| 1881 | @Override |
| 1882 | public int verifyEncryptionPassword(String password) throws RemoteException { |
| 1883 | // Only the system process is permitted to validate passwords |
| 1884 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 1885 | throw new SecurityException("no permission to access the crypt keeper"); |
| 1886 | } |
| 1887 | |
| 1888 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 1889 | "no permission to access the crypt keeper"); |
| 1890 | |
| 1891 | if (TextUtils.isEmpty(password)) { |
| 1892 | throw new IllegalArgumentException("password cannot be empty"); |
| 1893 | } |
| 1894 | |
| 1895 | waitForReady(); |
| 1896 | |
| 1897 | if (DEBUG_EVENTS) { |
| 1898 | Slog.i(TAG, "validating encryption password..."); |
| 1899 | } |
| 1900 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1901 | final NativeDaemonEvent event; |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 1902 | try { |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 1903 | event = mConnector.execute("cryptfs", "verifypw", new SensitiveArg(toHex(password))); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1904 | Slog.i(TAG, "cryptfs verifypw => " + event.getMessage()); |
| 1905 | return Integer.parseInt(event.getMessage()); |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 1906 | } catch (NativeDaemonConnectorException e) { |
| 1907 | // Encryption failed |
| 1908 | return e.getCode(); |
| 1909 | } |
| 1910 | } |
| 1911 | |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 1912 | /** |
| 1913 | * Get the type of encryption used to encrypt the master key. |
| 1914 | * @return The type, one of the CRYPT_TYPE_XXX consts from StorageManager. |
| 1915 | */ |
| 1916 | @Override |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 1917 | public int getPasswordType() { |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 1918 | |
| 1919 | waitForReady(); |
| 1920 | |
| 1921 | final NativeDaemonEvent event; |
| 1922 | try { |
| 1923 | event = mConnector.execute("cryptfs", "getpwtype"); |
| 1924 | for (int i = 0; i < CRYPTO_TYPES.length; ++i) { |
| 1925 | if (CRYPTO_TYPES[i].equals(event.getMessage())) |
| 1926 | return i; |
| 1927 | } |
| 1928 | |
| 1929 | throw new IllegalStateException("unexpected return from cryptfs"); |
| 1930 | } catch (NativeDaemonConnectorException e) { |
| 1931 | throw e.rethrowAsParcelableException(); |
| 1932 | } |
| 1933 | } |
| 1934 | |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 1935 | /** |
| 1936 | * Set a field in the crypto header. |
| 1937 | * @param field field to set |
| 1938 | * @param contents contents to set in field |
| 1939 | */ |
| 1940 | @Override |
| 1941 | public void setField(String field, String contents) throws RemoteException { |
| 1942 | |
| 1943 | waitForReady(); |
| 1944 | |
| 1945 | final NativeDaemonEvent event; |
| 1946 | try { |
| 1947 | event = mConnector.execute("cryptfs", "setfield", field, contents); |
| 1948 | } catch (NativeDaemonConnectorException e) { |
| 1949 | throw e.rethrowAsParcelableException(); |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | /** |
| 1954 | * Gets a field from the crypto header. |
| 1955 | * @param field field to get |
| 1956 | * @return contents of field |
| 1957 | */ |
| 1958 | @Override |
| 1959 | public String getField(String field) throws RemoteException { |
| 1960 | |
| 1961 | waitForReady(); |
| 1962 | |
| 1963 | final NativeDaemonEvent event; |
| 1964 | try { |
| 1965 | final String[] contents = NativeDaemonEvent.filterMessageList( |
| 1966 | mConnector.executeForList("cryptfs", "getfield", field), |
| 1967 | VoldResponseCode.CryptfsGetfieldResult); |
| 1968 | String result = new String(); |
| 1969 | for (String content : contents) { |
| 1970 | result += content; |
| 1971 | } |
| 1972 | return result; |
| 1973 | } catch (NativeDaemonConnectorException e) { |
| 1974 | throw e.rethrowAsParcelableException(); |
| 1975 | } |
| 1976 | } |
| 1977 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1978 | @Override |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1979 | public String getPassword() throws RemoteException { |
| 1980 | if (!isReady()) { |
| 1981 | return new String(); |
| 1982 | } |
| 1983 | |
| 1984 | final NativeDaemonEvent event; |
| 1985 | try { |
| 1986 | event = mConnector.execute("cryptfs", "getpw"); |
Paul Lawrence | 24063b5 | 2015-01-06 13:11:23 -0800 | [diff] [blame] | 1987 | if ("-1".equals(event.getMessage())) { |
| 1988 | // -1 equals no password |
| 1989 | return null; |
| 1990 | } |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1991 | return fromHex(event.getMessage()); |
| 1992 | } catch (NativeDaemonConnectorException e) { |
| 1993 | throw e.rethrowAsParcelableException(); |
Paul Lawrence | 24063b5 | 2015-01-06 13:11:23 -0800 | [diff] [blame] | 1994 | } catch (IllegalArgumentException e) { |
| 1995 | Slog.e(TAG, "Invalid response to getPassword"); |
| 1996 | return null; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | @Override |
| 2001 | public void clearPassword() throws RemoteException { |
| 2002 | if (!isReady()) { |
| 2003 | return; |
| 2004 | } |
| 2005 | |
| 2006 | final NativeDaemonEvent event; |
| 2007 | try { |
| 2008 | event = mConnector.execute("cryptfs", "clearpw"); |
| 2009 | } catch (NativeDaemonConnectorException e) { |
| 2010 | throw e.rethrowAsParcelableException(); |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | @Override |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2015 | public int mkdirs(String callingPkg, String appPath) { |
| 2016 | final int userId = UserHandle.getUserId(Binder.getCallingUid()); |
| 2017 | final UserEnvironment userEnv = new UserEnvironment(userId); |
| 2018 | |
| 2019 | // Validate that reported package name belongs to caller |
| 2020 | final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService( |
| 2021 | Context.APP_OPS_SERVICE); |
| 2022 | appOps.checkPackage(Binder.getCallingUid(), callingPkg); |
| 2023 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2024 | File appFile = null; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2025 | try { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2026 | appFile = new File(appPath).getCanonicalFile(); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2027 | } catch (IOException e) { |
| 2028 | Slog.e(TAG, "Failed to resolve " + appPath + ": " + e); |
| 2029 | return -1; |
| 2030 | } |
| 2031 | |
| 2032 | // Try translating the app path into a vold path, but require that it |
| 2033 | // belong to the calling package. |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2034 | if (FileUtils.contains(userEnv.buildExternalStorageAppDataDirs(callingPkg), appFile) || |
| 2035 | FileUtils.contains(userEnv.buildExternalStorageAppObbDirs(callingPkg), appFile) || |
| 2036 | FileUtils.contains(userEnv.buildExternalStorageAppMediaDirs(callingPkg), appFile)) { |
| 2037 | appPath = appFile.getAbsolutePath(); |
| 2038 | if (!appPath.endsWith("/")) { |
| 2039 | appPath = appPath + "/"; |
| 2040 | } |
| 2041 | |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2042 | try { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2043 | mConnector.execute("volume", "mkdirs", appPath); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2044 | return 0; |
| 2045 | } catch (NativeDaemonConnectorException e) { |
| 2046 | return e.getCode(); |
| 2047 | } |
| 2048 | } |
| 2049 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2050 | throw new SecurityException("Invalid mkdirs path: " + appFile); |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | @Override |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2054 | public StorageVolume[] getVolumeList(int userId) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2055 | final ArrayList<StorageVolume> res = new ArrayList<>(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2056 | boolean foundPrimary = false; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2057 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2058 | synchronized (mLock) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2059 | for (int i = 0; i < mVolumes.size(); i++) { |
| 2060 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2061 | if (vol.isVisibleToUser(userId)) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2062 | final StorageVolume userVol = vol.buildStorageVolume(mContext, userId); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2063 | if (vol.isPrimary()) { |
| 2064 | res.add(0, userVol); |
| 2065 | foundPrimary = true; |
| 2066 | } else { |
| 2067 | res.add(userVol); |
| 2068 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 2069 | } |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 2070 | } |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 2071 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2072 | |
| 2073 | if (!foundPrimary) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2074 | Log.w(TAG, "No primary storage defined yet; hacking together a stub"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2075 | |
| 2076 | final boolean primaryPhysical = SystemProperties.getBoolean( |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2077 | StorageManager.PROP_PRIMARY_PHYSICAL, false); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2078 | |
| 2079 | final String id = "stub_primary"; |
| 2080 | final File path = Environment.getLegacyExternalStorageDirectory(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2081 | final String description = mContext.getString(android.R.string.unknownName); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2082 | final boolean primary = true; |
| 2083 | final boolean removable = primaryPhysical; |
| 2084 | final boolean emulated = !primaryPhysical; |
| 2085 | final long mtpReserveSize = 0L; |
| 2086 | final boolean allowMassStorage = false; |
| 2087 | final long maxFileSize = 0L; |
| 2088 | final UserHandle owner = new UserHandle(userId); |
| 2089 | final String uuid = null; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2090 | final String state = Environment.MEDIA_REMOVED; |
| 2091 | |
| 2092 | res.add(0, new StorageVolume(id, MtpStorage.getStorageIdForIndex(0), path, |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2093 | description, primary, removable, emulated, mtpReserveSize, |
| 2094 | allowMassStorage, maxFileSize, owner, uuid, state)); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | return res.toArray(new StorageVolume[res.size()]); |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2100 | @Override |
| 2101 | public DiskInfo[] getDisks() { |
| 2102 | synchronized (mLock) { |
| 2103 | final DiskInfo[] res = new DiskInfo[mDisks.size()]; |
| 2104 | for (int i = 0; i < mDisks.size(); i++) { |
| 2105 | res[i] = mDisks.valueAt(i); |
| 2106 | } |
| 2107 | return res; |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | @Override |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2112 | public VolumeInfo[] getVolumes(int flags) { |
| 2113 | if ((flags & StorageManager.FLAG_ALL_METADATA) != 0) { |
| 2114 | // TODO: implement support for returning all metadata |
| 2115 | throw new UnsupportedOperationException(); |
| 2116 | } |
| 2117 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2118 | synchronized (mLock) { |
| 2119 | final VolumeInfo[] res = new VolumeInfo[mVolumes.size()]; |
| 2120 | for (int i = 0; i < mVolumes.size(); i++) { |
| 2121 | res[i] = mVolumes.valueAt(i); |
| 2122 | } |
| 2123 | return res; |
| 2124 | } |
| 2125 | } |
| 2126 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2127 | private void addObbStateLocked(ObbState obbState) throws RemoteException { |
| 2128 | final IBinder binder = obbState.getBinder(); |
| 2129 | List<ObbState> obbStates = mObbMounts.get(binder); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 2130 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2131 | if (obbStates == null) { |
| 2132 | obbStates = new ArrayList<ObbState>(); |
| 2133 | mObbMounts.put(binder, obbStates); |
| 2134 | } else { |
| 2135 | for (final ObbState o : obbStates) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2136 | if (o.rawPath.equals(obbState.rawPath)) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2137 | throw new IllegalStateException("Attempt to add ObbState twice. " |
| 2138 | + "This indicates an error in the MountService logic."); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 2139 | } |
| 2140 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2141 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2142 | |
| 2143 | obbStates.add(obbState); |
| 2144 | try { |
| 2145 | obbState.link(); |
| 2146 | } catch (RemoteException e) { |
| 2147 | /* |
| 2148 | * The binder died before we could link it, so clean up our state |
| 2149 | * and return failure. |
| 2150 | */ |
| 2151 | obbStates.remove(obbState); |
| 2152 | if (obbStates.isEmpty()) { |
| 2153 | mObbMounts.remove(binder); |
| 2154 | } |
| 2155 | |
| 2156 | // Rethrow the error so mountObb can get it |
| 2157 | throw e; |
| 2158 | } |
| 2159 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2160 | mObbPathToStateMap.put(obbState.rawPath, obbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2163 | private void removeObbStateLocked(ObbState obbState) { |
| 2164 | final IBinder binder = obbState.getBinder(); |
| 2165 | final List<ObbState> obbStates = mObbMounts.get(binder); |
| 2166 | if (obbStates != null) { |
| 2167 | if (obbStates.remove(obbState)) { |
| 2168 | obbState.unlink(); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 2169 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2170 | if (obbStates.isEmpty()) { |
| 2171 | mObbMounts.remove(binder); |
| 2172 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2173 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2174 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2175 | mObbPathToStateMap.remove(obbState.rawPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2178 | private class ObbActionHandler extends Handler { |
| 2179 | private boolean mBound = false; |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 2180 | private final List<ObbAction> mActions = new LinkedList<ObbAction>(); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2181 | |
| 2182 | ObbActionHandler(Looper l) { |
| 2183 | super(l); |
| 2184 | } |
| 2185 | |
| 2186 | @Override |
| 2187 | public void handleMessage(Message msg) { |
| 2188 | switch (msg.what) { |
| 2189 | case OBB_RUN_ACTION: { |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 2190 | final ObbAction action = (ObbAction) msg.obj; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2191 | |
| 2192 | if (DEBUG_OBB) |
| 2193 | Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString()); |
| 2194 | |
| 2195 | // If a bind was already initiated we don't really |
| 2196 | // need to do anything. The pending install |
| 2197 | // will be processed later on. |
| 2198 | if (!mBound) { |
| 2199 | // If this is the only one pending we might |
| 2200 | // have to bind to the service again. |
| 2201 | if (!connectToService()) { |
| 2202 | Slog.e(TAG, "Failed to bind to media container service"); |
| 2203 | action.handleError(); |
| 2204 | return; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2205 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2206 | } |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 2207 | |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 2208 | mActions.add(action); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2209 | break; |
| 2210 | } |
| 2211 | case OBB_MCS_BOUND: { |
| 2212 | if (DEBUG_OBB) |
| 2213 | Slog.i(TAG, "OBB_MCS_BOUND"); |
| 2214 | if (msg.obj != null) { |
| 2215 | mContainerService = (IMediaContainerService) msg.obj; |
| 2216 | } |
| 2217 | if (mContainerService == null) { |
| 2218 | // Something seriously wrong. Bail out |
| 2219 | Slog.e(TAG, "Cannot bind to media container service"); |
| 2220 | for (ObbAction action : mActions) { |
| 2221 | // Indicate service bind error |
| 2222 | action.handleError(); |
| 2223 | } |
| 2224 | mActions.clear(); |
| 2225 | } else if (mActions.size() > 0) { |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 2226 | final ObbAction action = mActions.get(0); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2227 | if (action != null) { |
| 2228 | action.execute(this); |
| 2229 | } |
| 2230 | } else { |
| 2231 | // Should never happen ideally. |
| 2232 | Slog.w(TAG, "Empty queue"); |
| 2233 | } |
| 2234 | break; |
| 2235 | } |
| 2236 | case OBB_MCS_RECONNECT: { |
| 2237 | if (DEBUG_OBB) |
| 2238 | Slog.i(TAG, "OBB_MCS_RECONNECT"); |
| 2239 | if (mActions.size() > 0) { |
| 2240 | if (mBound) { |
| 2241 | disconnectService(); |
| 2242 | } |
| 2243 | if (!connectToService()) { |
| 2244 | Slog.e(TAG, "Failed to bind to media container service"); |
| 2245 | for (ObbAction action : mActions) { |
| 2246 | // Indicate service bind error |
| 2247 | action.handleError(); |
| 2248 | } |
| 2249 | mActions.clear(); |
| 2250 | } |
| 2251 | } |
| 2252 | break; |
| 2253 | } |
| 2254 | case OBB_MCS_UNBIND: { |
| 2255 | if (DEBUG_OBB) |
| 2256 | Slog.i(TAG, "OBB_MCS_UNBIND"); |
| 2257 | |
| 2258 | // Delete pending install |
| 2259 | if (mActions.size() > 0) { |
| 2260 | mActions.remove(0); |
| 2261 | } |
| 2262 | if (mActions.size() == 0) { |
| 2263 | if (mBound) { |
| 2264 | disconnectService(); |
| 2265 | } |
| 2266 | } else { |
| 2267 | // There are more pending requests in queue. |
| 2268 | // Just post MCS_BOUND message to trigger processing |
| 2269 | // of next pending install. |
| 2270 | mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND); |
| 2271 | } |
| 2272 | break; |
| 2273 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2274 | case OBB_FLUSH_MOUNT_STATE: { |
| 2275 | final String path = (String) msg.obj; |
| 2276 | |
| 2277 | if (DEBUG_OBB) |
| 2278 | Slog.i(TAG, "Flushing all OBB state for path " + path); |
| 2279 | |
| 2280 | synchronized (mObbMounts) { |
| 2281 | final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>(); |
| 2282 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2283 | final Iterator<ObbState> i = mObbPathToStateMap.values().iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2284 | while (i.hasNext()) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2285 | final ObbState state = i.next(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2286 | |
| 2287 | /* |
| 2288 | * If this entry's source file is in the volume path |
| 2289 | * that got unmounted, remove it because it's no |
| 2290 | * longer valid. |
| 2291 | */ |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2292 | if (state.canonicalPath.startsWith(path)) { |
| 2293 | obbStatesToRemove.add(state); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | for (final ObbState obbState : obbStatesToRemove) { |
| 2298 | if (DEBUG_OBB) |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2299 | Slog.i(TAG, "Removing state for " + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2300 | |
| 2301 | removeObbStateLocked(obbState); |
| 2302 | |
| 2303 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2304 | obbState.token.onObbResult(obbState.rawPath, obbState.nonce, |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2305 | OnObbStateChangeListener.UNMOUNTED); |
| 2306 | } catch (RemoteException e) { |
| 2307 | Slog.i(TAG, "Couldn't send unmount notification for OBB: " |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2308 | + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | } |
| 2312 | break; |
| 2313 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | private boolean connectToService() { |
| 2318 | if (DEBUG_OBB) |
| 2319 | Slog.i(TAG, "Trying to bind to DefaultContainerService"); |
| 2320 | |
| 2321 | Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 2322 | if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) { |
| 2323 | mBound = true; |
| 2324 | return true; |
| 2325 | } |
| 2326 | return false; |
| 2327 | } |
| 2328 | |
| 2329 | private void disconnectService() { |
| 2330 | mContainerService = null; |
| 2331 | mBound = false; |
| 2332 | mContext.unbindService(mDefContainerConn); |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | abstract class ObbAction { |
| 2337 | private static final int MAX_RETRIES = 3; |
| 2338 | private int mRetries; |
| 2339 | |
| 2340 | ObbState mObbState; |
| 2341 | |
| 2342 | ObbAction(ObbState obbState) { |
| 2343 | mObbState = obbState; |
| 2344 | } |
| 2345 | |
| 2346 | public void execute(ObbActionHandler handler) { |
| 2347 | try { |
| 2348 | if (DEBUG_OBB) |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2349 | Slog.i(TAG, "Starting to execute action: " + toString()); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2350 | mRetries++; |
| 2351 | if (mRetries > MAX_RETRIES) { |
| 2352 | 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] | 2353 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2354 | handleError(); |
| 2355 | return; |
| 2356 | } else { |
| 2357 | handleExecute(); |
| 2358 | if (DEBUG_OBB) |
| 2359 | Slog.i(TAG, "Posting install MCS_UNBIND"); |
| 2360 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
| 2361 | } |
| 2362 | } catch (RemoteException e) { |
| 2363 | if (DEBUG_OBB) |
| 2364 | Slog.i(TAG, "Posting install MCS_RECONNECT"); |
| 2365 | mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT); |
| 2366 | } catch (Exception e) { |
| 2367 | if (DEBUG_OBB) |
| 2368 | Slog.d(TAG, "Error handling OBB action", e); |
| 2369 | handleError(); |
Kenny Root | 17eb6fb | 2010-10-06 15:02:52 -0700 | [diff] [blame] | 2370 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 2374 | abstract void handleExecute() throws RemoteException, IOException; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2375 | abstract void handleError(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2376 | |
| 2377 | protected ObbInfo getObbInfo() throws IOException { |
| 2378 | ObbInfo obbInfo; |
| 2379 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2380 | obbInfo = mContainerService.getObbInfo(mObbState.ownerPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2381 | } catch (RemoteException e) { |
| 2382 | Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for " |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2383 | + mObbState.ownerPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2384 | obbInfo = null; |
| 2385 | } |
| 2386 | if (obbInfo == null) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2387 | throw new IOException("Couldn't read OBB file: " + mObbState.ownerPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2388 | } |
| 2389 | return obbInfo; |
| 2390 | } |
| 2391 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2392 | protected void sendNewStatusOrIgnore(int status) { |
| 2393 | if (mObbState == null || mObbState.token == null) { |
| 2394 | return; |
| 2395 | } |
| 2396 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2397 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2398 | mObbState.token.onObbResult(mObbState.rawPath, mObbState.nonce, status); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2399 | } catch (RemoteException e) { |
| 2400 | Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged"); |
| 2401 | } |
| 2402 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | class MountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2406 | private final String mKey; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2407 | private final int mCallingUid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2408 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2409 | MountObbAction(ObbState obbState, String key, int callingUid) { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2410 | super(obbState); |
| 2411 | mKey = key; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2412 | mCallingUid = callingUid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2413 | } |
| 2414 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2415 | @Override |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 2416 | public void handleExecute() throws IOException, RemoteException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2417 | waitForReady(); |
| 2418 | warnOnNotMounted(); |
| 2419 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2420 | final ObbInfo obbInfo = getObbInfo(); |
| 2421 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2422 | if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mCallingUid)) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2423 | Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename |
| 2424 | + " which is owned by " + obbInfo.packageName); |
| 2425 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED); |
| 2426 | return; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2429 | final boolean isMounted; |
| 2430 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2431 | isMounted = mObbPathToStateMap.containsKey(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2432 | } |
| 2433 | if (isMounted) { |
| 2434 | Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename); |
| 2435 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED); |
| 2436 | return; |
| 2437 | } |
| 2438 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2439 | final String hashedKey; |
| 2440 | if (mKey == null) { |
| 2441 | hashedKey = "none"; |
| 2442 | } else { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2443 | try { |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 2444 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 2445 | |
| 2446 | KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt, |
| 2447 | PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE); |
| 2448 | SecretKey key = factory.generateSecret(ks); |
| 2449 | BigInteger bi = new BigInteger(key.getEncoded()); |
| 2450 | hashedKey = bi.toString(16); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2451 | } catch (NoSuchAlgorithmException e) { |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 2452 | Slog.e(TAG, "Could not load PBKDF2 algorithm", e); |
| 2453 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
| 2454 | return; |
| 2455 | } catch (InvalidKeySpecException e) { |
| 2456 | Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e); |
| 2457 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2458 | return; |
| 2459 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2460 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2461 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2462 | int rc = StorageResultCode.OperationSucceeded; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2463 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 2464 | mConnector.execute("obb", "mount", mObbState.voldPath, new SensitiveArg(hashedKey), |
| 2465 | mObbState.ownerGid); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2466 | } catch (NativeDaemonConnectorException e) { |
| 2467 | int code = e.getCode(); |
| 2468 | if (code != VoldResponseCode.OpFailedStorageBusy) { |
| 2469 | rc = StorageResultCode.OperationFailedInternalError; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2470 | } |
| 2471 | } |
| 2472 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2473 | if (rc == StorageResultCode.OperationSucceeded) { |
| 2474 | if (DEBUG_OBB) |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2475 | Slog.d(TAG, "Successfully mounted OBB " + mObbState.voldPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2476 | |
| 2477 | synchronized (mObbMounts) { |
| 2478 | addObbStateLocked(mObbState); |
| 2479 | } |
| 2480 | |
| 2481 | sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2482 | } else { |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 2483 | Slog.e(TAG, "Couldn't mount OBB file: " + rc); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2484 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2485 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2486 | } |
| 2487 | } |
| 2488 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2489 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2490 | public void handleError() { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2491 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2492 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2493 | |
| 2494 | @Override |
| 2495 | public String toString() { |
| 2496 | StringBuilder sb = new StringBuilder(); |
| 2497 | sb.append("MountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2498 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2499 | sb.append('}'); |
| 2500 | return sb.toString(); |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | class UnmountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2505 | private final boolean mForceUnmount; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2506 | |
| 2507 | UnmountObbAction(ObbState obbState, boolean force) { |
| 2508 | super(obbState); |
| 2509 | mForceUnmount = force; |
| 2510 | } |
| 2511 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2512 | @Override |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2513 | public void handleExecute() throws IOException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2514 | waitForReady(); |
| 2515 | warnOnNotMounted(); |
| 2516 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2517 | final ObbInfo obbInfo = getObbInfo(); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2518 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2519 | final ObbState existingState; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2520 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2521 | existingState = mObbPathToStateMap.get(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2522 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2523 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2524 | if (existingState == null) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2525 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED); |
| 2526 | return; |
| 2527 | } |
| 2528 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2529 | if (existingState.ownerGid != mObbState.ownerGid) { |
| 2530 | Slog.w(TAG, "Permission denied attempting to unmount OBB " + existingState.rawPath |
| 2531 | + " (owned by GID " + existingState.ownerGid + ")"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2532 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED); |
| 2533 | return; |
| 2534 | } |
| 2535 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2536 | int rc = StorageResultCode.OperationSucceeded; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2537 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2538 | final Command cmd = new Command("obb", "unmount", mObbState.voldPath); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2539 | if (mForceUnmount) { |
| 2540 | cmd.appendArg("force"); |
| 2541 | } |
| 2542 | mConnector.execute(cmd); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2543 | } catch (NativeDaemonConnectorException e) { |
| 2544 | int code = e.getCode(); |
| 2545 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 2546 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 2547 | } else if (code == VoldResponseCode.OpFailedStorageNotFound) { |
| 2548 | // If it's not mounted then we've already won. |
| 2549 | rc = StorageResultCode.OperationSucceeded; |
| 2550 | } else { |
| 2551 | rc = StorageResultCode.OperationFailedInternalError; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2552 | } |
| 2553 | } |
| 2554 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2555 | if (rc == StorageResultCode.OperationSucceeded) { |
| 2556 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2557 | removeObbStateLocked(existingState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2560 | sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2561 | } else { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2562 | Slog.w(TAG, "Could not unmount OBB: " + existingState); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2563 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2564 | } |
| 2565 | } |
| 2566 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2567 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2568 | public void handleError() { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2569 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | @Override |
| 2573 | public String toString() { |
| 2574 | StringBuilder sb = new StringBuilder(); |
| 2575 | sb.append("UnmountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2576 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2577 | sb.append(",force="); |
| 2578 | sb.append(mForceUnmount); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2579 | sb.append('}'); |
| 2580 | return sb.toString(); |
| 2581 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2582 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2583 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 2584 | @VisibleForTesting |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2585 | public static String buildObbPath(final String canonicalPath, int userId, boolean forVold) { |
| 2586 | // TODO: allow caller to provide Environment for full testing |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2587 | // TODO: extend to support OBB mounts on secondary external storage |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2588 | |
| 2589 | // Only adjust paths when storage is emulated |
| 2590 | if (!Environment.isExternalStorageEmulated()) { |
| 2591 | return canonicalPath; |
| 2592 | } |
| 2593 | |
| 2594 | String path = canonicalPath.toString(); |
| 2595 | |
| 2596 | // First trim off any external storage prefix |
| 2597 | final UserEnvironment userEnv = new UserEnvironment(userId); |
| 2598 | |
| 2599 | // /storage/emulated/0 |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2600 | final String externalPath = userEnv.getExternalStorageDirectory().getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2601 | // /storage/emulated_legacy |
| 2602 | final String legacyExternalPath = Environment.getLegacyExternalStorageDirectory() |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2603 | .getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2604 | |
| 2605 | if (path.startsWith(externalPath)) { |
| 2606 | path = path.substring(externalPath.length() + 1); |
| 2607 | } else if (path.startsWith(legacyExternalPath)) { |
| 2608 | path = path.substring(legacyExternalPath.length() + 1); |
| 2609 | } else { |
| 2610 | return canonicalPath; |
| 2611 | } |
| 2612 | |
| 2613 | // Handle special OBB paths on emulated storage |
| 2614 | final String obbPath = "Android/obb"; |
| 2615 | if (path.startsWith(obbPath)) { |
| 2616 | path = path.substring(obbPath.length() + 1); |
| 2617 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2618 | final UserEnvironment ownerEnv = new UserEnvironment(UserHandle.USER_OWNER); |
| 2619 | return new File(ownerEnv.buildExternalStorageAndroidObbDirs()[0], path) |
| 2620 | .getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | // Handle normal external storage paths |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2624 | return new File(userEnv.getExternalStorageDirectory(), path).getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2627 | private static class Callbacks extends Handler { |
| 2628 | private static final int MSG_STORAGE_STATE_CHANGED = 1; |
| 2629 | private static final int MSG_VOLUME_STATE_CHANGED = 2; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2630 | private static final int MSG_VOLUME_METADATA_CHANGED = 3; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2631 | |
| 2632 | private final RemoteCallbackList<IMountServiceListener> |
| 2633 | mCallbacks = new RemoteCallbackList<>(); |
| 2634 | |
| 2635 | public Callbacks(Looper looper) { |
| 2636 | super(looper); |
| 2637 | } |
| 2638 | |
| 2639 | public void register(IMountServiceListener callback) { |
| 2640 | mCallbacks.register(callback); |
| 2641 | } |
| 2642 | |
| 2643 | public void unregister(IMountServiceListener callback) { |
| 2644 | mCallbacks.unregister(callback); |
| 2645 | } |
| 2646 | |
| 2647 | @Override |
| 2648 | public void handleMessage(Message msg) { |
| 2649 | final SomeArgs args = (SomeArgs) msg.obj; |
| 2650 | final int n = mCallbacks.beginBroadcast(); |
| 2651 | for (int i = 0; i < n; i++) { |
| 2652 | final IMountServiceListener callback = mCallbacks.getBroadcastItem(i); |
| 2653 | try { |
| 2654 | invokeCallback(callback, msg.what, args); |
| 2655 | } catch (RemoteException ignored) { |
| 2656 | } |
| 2657 | } |
| 2658 | mCallbacks.finishBroadcast(); |
| 2659 | args.recycle(); |
| 2660 | } |
| 2661 | |
| 2662 | private void invokeCallback(IMountServiceListener callback, int what, SomeArgs args) |
| 2663 | throws RemoteException { |
| 2664 | switch (what) { |
| 2665 | case MSG_STORAGE_STATE_CHANGED: { |
| 2666 | callback.onStorageStateChanged((String) args.arg1, (String) args.arg2, |
| 2667 | (String) args.arg3); |
| 2668 | break; |
| 2669 | } |
| 2670 | case MSG_VOLUME_STATE_CHANGED: { |
| 2671 | callback.onVolumeStateChanged((VolumeInfo) args.arg1, args.argi2, args.argi3); |
| 2672 | break; |
| 2673 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2674 | case MSG_VOLUME_METADATA_CHANGED: { |
| 2675 | callback.onVolumeMetadataChanged((VolumeInfo) args.arg1); |
| 2676 | break; |
| 2677 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | private void notifyStorageStateChanged(String path, String oldState, String newState) { |
| 2682 | final SomeArgs args = SomeArgs.obtain(); |
| 2683 | args.arg1 = path; |
| 2684 | args.arg2 = oldState; |
| 2685 | args.arg3 = newState; |
| 2686 | obtainMessage(MSG_STORAGE_STATE_CHANGED, args).sendToTarget(); |
| 2687 | } |
| 2688 | |
| 2689 | private void notifyVolumeStateChanged(VolumeInfo vol, int oldState, int newState) { |
| 2690 | final SomeArgs args = SomeArgs.obtain(); |
| 2691 | args.arg1 = vol; |
| 2692 | args.argi2 = oldState; |
| 2693 | args.argi3 = newState; |
| 2694 | obtainMessage(MSG_VOLUME_STATE_CHANGED, args).sendToTarget(); |
| 2695 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2696 | |
| 2697 | private void notifyVolumeMetadataChanged(VolumeInfo vol) { |
| 2698 | final SomeArgs args = SomeArgs.obtain(); |
| 2699 | args.arg1 = vol; |
| 2700 | obtainMessage(MSG_VOLUME_METADATA_CHANGED, args).sendToTarget(); |
| 2701 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2704 | @Override |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2705 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 2706 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 2707 | |
| 2708 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ", 160); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2709 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2710 | synchronized (mObbMounts) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2711 | pw.println("mObbMounts:"); |
| 2712 | pw.increaseIndent(); |
| 2713 | final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet() |
| 2714 | .iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2715 | while (binders.hasNext()) { |
| 2716 | Entry<IBinder, List<ObbState>> e = binders.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2717 | pw.println(e.getKey() + ":"); |
| 2718 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2719 | final List<ObbState> obbStates = e.getValue(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2720 | for (final ObbState obbState : obbStates) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2721 | pw.println(obbState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2722 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2723 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2724 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2725 | pw.decreaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2726 | |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2727 | pw.println(); |
| 2728 | pw.println("mObbPathToStateMap:"); |
| 2729 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2730 | final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator(); |
| 2731 | while (maps.hasNext()) { |
| 2732 | final Entry<String, ObbState> e = maps.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2733 | pw.print(e.getKey()); |
| 2734 | pw.print(" -> "); |
| 2735 | pw.println(e.getValue()); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2736 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2737 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2738 | } |
Kenny Root | 4161f9b | 2011-07-13 09:48:33 -0700 | [diff] [blame] | 2739 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2740 | synchronized (mLock) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2741 | pw.println(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2742 | pw.println("Disks:"); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2743 | pw.increaseIndent(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2744 | for (int i = 0; i < mDisks.size(); i++) { |
| 2745 | final DiskInfo disk = mDisks.valueAt(i); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2746 | disk.dump(pw); |
| 2747 | } |
| 2748 | pw.decreaseIndent(); |
| 2749 | |
| 2750 | pw.println(); |
| 2751 | pw.println("Volumes:"); |
| 2752 | pw.increaseIndent(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 2753 | for (int i = 0; i < mVolumes.size(); i++) { |
| 2754 | final VolumeInfo vol = mVolumes.valueAt(i); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2755 | vol.dump(pw); |
Kenny Root | 4161f9b | 2011-07-13 09:48:33 -0700 | [diff] [blame] | 2756 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2757 | pw.decreaseIndent(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 2758 | |
| 2759 | pw.println(); |
| 2760 | pw.println("Metadata:"); |
| 2761 | pw.increaseIndent(); |
| 2762 | for (int i = 0; i < mMetadata.size(); i++) { |
| 2763 | final VolumeMetadata meta = mMetadata.valueAt(i); |
| 2764 | meta.dump(pw); |
| 2765 | } |
| 2766 | pw.decreaseIndent(); |
Kenny Root | 4161f9b | 2011-07-13 09:48:33 -0700 | [diff] [blame] | 2767 | } |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 2768 | |
| 2769 | pw.println(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2770 | pw.println("mConnection:"); |
| 2771 | pw.increaseIndent(); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 2772 | mConnector.dump(fd, pw, args); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2773 | pw.decreaseIndent(); |
Christopher Tate | 7265abe | 2014-11-21 13:54:45 -0800 | [diff] [blame] | 2774 | |
| 2775 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 2776 | |
| 2777 | pw.println(); |
| 2778 | pw.print("Last maintenance: "); |
| 2779 | pw.println(sdf.format(new Date(mLastMaintenance))); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2780 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2781 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 2782 | /** {@inheritDoc} */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 2783 | @Override |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 2784 | public void monitor() { |
| 2785 | if (mConnector != null) { |
| 2786 | mConnector.monitor(); |
| 2787 | } |
| 2788 | } |
| 2789 | } |