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