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 | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 20 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 21 | import android.Manifest; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 22 | import android.app.AppOpsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 24 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 28 | import android.content.ServiceConnection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 30 | import android.content.pm.UserInfo; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 31 | import android.content.res.ObbInfo; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 32 | import android.content.res.Resources; |
| 33 | import android.content.res.TypedArray; |
| 34 | import android.content.res.XmlResourceParser; |
Mike Lockwood | ecedfdc | 2011-06-08 15:11:59 -0700 | [diff] [blame] | 35 | import android.hardware.usb.UsbManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.net.Uri; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 37 | import android.os.Binder; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 38 | import android.os.Environment; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 39 | import android.os.Environment.UserEnvironment; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 40 | import android.os.Handler; |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 41 | import android.os.HandlerThread; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 42 | import android.os.IBinder; |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 43 | import android.os.Looper; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 44 | import android.os.Message; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 45 | import android.os.RemoteException; |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 46 | import android.os.ServiceManager; |
Svetoslav | f23b64d | 2013-04-25 14:45:54 -0700 | [diff] [blame] | 47 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 49 | import android.os.UserHandle; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 50 | import android.os.storage.IMountService; |
| 51 | import android.os.storage.IMountServiceListener; |
| 52 | import android.os.storage.IMountShutdownObserver; |
| 53 | import android.os.storage.IObbActionListener; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 54 | import android.os.storage.OnObbStateChangeListener; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 55 | import android.os.storage.StorageResultCode; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 56 | import android.os.storage.StorageVolume; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 57 | import android.text.TextUtils; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 58 | import android.util.AttributeSet; |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 59 | import android.util.Slog; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 60 | import android.util.Xml; |
| 61 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 62 | import com.android.internal.annotations.GuardedBy; |
| 63 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 64 | import com.android.internal.app.IMediaContainerService; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 65 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 66 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 67 | import com.android.internal.util.XmlUtils; |
| 68 | import com.android.server.NativeDaemonConnector.Command; |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 69 | import com.android.server.NativeDaemonConnector.SensitiveArg; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 70 | import com.android.server.am.ActivityManagerService; |
| 71 | import com.android.server.pm.PackageManagerService; |
| 72 | import com.android.server.pm.UserManagerService; |
| 73 | import com.google.android.collect.Lists; |
| 74 | import com.google.android.collect.Maps; |
| 75 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 76 | import org.xmlpull.v1.XmlPullParserException; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 77 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 78 | import java.io.File; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 79 | import java.io.FileDescriptor; |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 80 | import java.io.IOException; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 81 | import java.io.PrintWriter; |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 82 | import java.math.BigInteger; |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 83 | import java.security.NoSuchAlgorithmException; |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 84 | import java.security.spec.InvalidKeySpecException; |
| 85 | import java.security.spec.KeySpec; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 86 | import java.util.ArrayList; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 87 | import java.util.HashMap; |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 88 | import java.util.HashSet; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 89 | import java.util.Iterator; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 90 | import java.util.LinkedList; |
| 91 | import java.util.List; |
| 92 | import java.util.Map; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 93 | import java.util.Map.Entry; |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 94 | import java.util.concurrent.CountDownLatch; |
| 95 | import java.util.concurrent.TimeUnit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 97 | import javax.crypto.SecretKey; |
| 98 | import javax.crypto.SecretKeyFactory; |
| 99 | import javax.crypto.spec.PBEKeySpec; |
| 100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | /** |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 102 | * MountService implements back-end services for platform storage |
| 103 | * management. |
| 104 | * @hide - Applications should use android.os.storage.StorageManager |
| 105 | * to access the MountService. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | */ |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 107 | class MountService extends IMountService.Stub |
| 108 | implements INativeDaemonConnectorCallbacks, Watchdog.Monitor { |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 109 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 110 | // TODO: listen for user creation/deletion |
| 111 | |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 112 | private static final boolean LOCAL_LOGD = false; |
| 113 | private static final boolean DEBUG_UNMOUNT = false; |
| 114 | private static final boolean DEBUG_EVENTS = false; |
Kenny Root | b7db272 | 2011-01-25 16:39:35 -0800 | [diff] [blame] | 115 | private static final boolean DEBUG_OBB = false; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 116 | |
Kenny Root | 07714d4 | 2011-08-17 17:49:28 -0700 | [diff] [blame] | 117 | // Disable this since it messes up long-running cryptfs operations. |
| 118 | private static final boolean WATCHDOG_ENABLE = false; |
| 119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | private static final String TAG = "MountService"; |
| 121 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 122 | private static final String VOLD_TAG = "VoldConnector"; |
| 123 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 124 | /** Maximum number of ASEC containers allowed to be mounted. */ |
| 125 | private static final int MAX_CONTAINERS = 250; |
| 126 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 127 | /* |
| 128 | * Internal vold volume state constants |
| 129 | */ |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 130 | class VolumeState { |
| 131 | public static final int Init = -1; |
| 132 | public static final int NoMedia = 0; |
| 133 | public static final int Idle = 1; |
| 134 | public static final int Pending = 2; |
| 135 | public static final int Checking = 3; |
| 136 | public static final int Mounted = 4; |
| 137 | public static final int Unmounting = 5; |
| 138 | public static final int Formatting = 6; |
| 139 | public static final int Shared = 7; |
| 140 | public static final int SharedMnt = 8; |
| 141 | } |
| 142 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 143 | /* |
| 144 | * Internal vold response code constants |
| 145 | */ |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 146 | class VoldResponseCode { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 147 | /* |
| 148 | * 100 series - Requestion action was initiated; expect another reply |
| 149 | * before proceeding with a new command. |
| 150 | */ |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 151 | public static final int VolumeListResult = 110; |
| 152 | public static final int AsecListResult = 111; |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 153 | public static final int StorageUsersListResult = 112; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 154 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 155 | /* |
| 156 | * 200 series - Requestion action has been successfully completed. |
| 157 | */ |
| 158 | public static final int ShareStatusResult = 210; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 159 | public static final int AsecPathResult = 211; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 160 | public static final int ShareEnabledResult = 212; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 161 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 162 | /* |
| 163 | * 400 series - Command was accepted, but the requested action |
| 164 | * did not take place. |
| 165 | */ |
| 166 | public static final int OpFailedNoMedia = 401; |
| 167 | public static final int OpFailedMediaBlank = 402; |
| 168 | public static final int OpFailedMediaCorrupt = 403; |
| 169 | public static final int OpFailedVolNotMounted = 404; |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 170 | public static final int OpFailedStorageBusy = 405; |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 171 | public static final int OpFailedStorageNotFound = 406; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * 600 series - Unsolicited broadcasts. |
| 175 | */ |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 176 | public static final int VolumeStateChange = 605; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 177 | public static final int VolumeUuidChange = 613; |
| 178 | public static final int VolumeUserLabelChange = 614; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 179 | public static final int VolumeDiskInserted = 630; |
| 180 | public static final int VolumeDiskRemoved = 631; |
| 181 | public static final int VolumeBadRemoval = 632; |
Svetoslav | f23b64d | 2013-04-25 14:45:54 -0700 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * 700 series - fstrim |
| 185 | */ |
| 186 | public static final int FstrimCompleted = 700; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 189 | private Context mContext; |
| 190 | private NativeDaemonConnector mConnector; |
| 191 | |
| 192 | private final Object mVolumesLock = new Object(); |
| 193 | |
| 194 | /** When defined, base template for user-specific {@link StorageVolume}. */ |
| 195 | private StorageVolume mEmulatedTemplate; |
| 196 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 197 | // TODO: separate storage volumes on per-user basis |
| 198 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 199 | @GuardedBy("mVolumesLock") |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 200 | private final ArrayList<StorageVolume> mVolumes = Lists.newArrayList(); |
| 201 | /** Map from path to {@link StorageVolume} */ |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 202 | @GuardedBy("mVolumesLock") |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 203 | private final HashMap<String, StorageVolume> mVolumesByPath = Maps.newHashMap(); |
| 204 | /** Map from path to state */ |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 205 | @GuardedBy("mVolumesLock") |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 206 | private final HashMap<String, String> mVolumeStates = Maps.newHashMap(); |
| 207 | |
| 208 | private volatile boolean mSystemReady = false; |
| 209 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 210 | private PackageManagerService mPms; |
| 211 | private boolean mUmsEnabling; |
Mike Lockwood | ecedfdc | 2011-06-08 15:11:59 -0700 | [diff] [blame] | 212 | private boolean mUmsAvailable = false; |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 213 | // Used as a lock for methods that register/unregister listeners. |
| 214 | final private ArrayList<MountServiceBinderListener> mListeners = |
| 215 | new ArrayList<MountServiceBinderListener>(); |
Jeff Sharkey | 0be607c | 2012-11-14 14:39:19 -0800 | [diff] [blame] | 216 | private final CountDownLatch mConnectedSignal = new CountDownLatch(1); |
| 217 | private final CountDownLatch mAsecsScanned = new CountDownLatch(1); |
San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 218 | private boolean mSendUmsConnectedOnBoot = false; |
Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 219 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 220 | /** |
| 221 | * Private hash of currently mounted secure containers. |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 222 | * Used as a lock in methods to manipulate secure containers. |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 223 | */ |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 224 | final private HashSet<String> mAsecMountSet = new HashSet<String>(); |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 225 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 226 | /** |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 227 | * The size of the crypto algorithm key in bits for OBB files. Currently |
| 228 | * Twofish is used which takes 128-bit keys. |
| 229 | */ |
| 230 | private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128; |
| 231 | |
| 232 | /** |
| 233 | * The number of times to run SHA1 in the PBKDF2 function for OBB files. |
| 234 | * 1024 is reasonably secure and not too slow. |
| 235 | */ |
| 236 | private static final int PBKDF2_HASH_ROUNDS = 1024; |
| 237 | |
| 238 | /** |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 239 | * Mounted OBB tracking information. Used to track the current state of all |
| 240 | * OBBs. |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 241 | */ |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 242 | final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 243 | |
| 244 | /** Map from raw paths to {@link ObbState}. */ |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 245 | final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>(); |
| 246 | |
| 247 | class ObbState implements IBinder.DeathRecipient { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 248 | public ObbState(String rawPath, String canonicalPath, int callingUid, |
| 249 | IObbActionListener token, int nonce) { |
| 250 | this.rawPath = rawPath; |
| 251 | this.canonicalPath = canonicalPath.toString(); |
| 252 | |
| 253 | final int userId = UserHandle.getUserId(callingUid); |
| 254 | this.ownerPath = buildObbPath(canonicalPath, userId, false); |
| 255 | this.voldPath = buildObbPath(canonicalPath, userId, true); |
| 256 | |
| 257 | this.ownerGid = UserHandle.getSharedAppGid(callingUid); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 258 | this.token = token; |
| 259 | this.nonce = nonce; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 262 | final String rawPath; |
| 263 | final String canonicalPath; |
| 264 | final String ownerPath; |
| 265 | final String voldPath; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 266 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 267 | final int ownerGid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 268 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 269 | // Token of remote Binder caller |
| 270 | final IObbActionListener token; |
| 271 | |
| 272 | // Identifier to pass back to the token |
| 273 | final int nonce; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 274 | |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 275 | public IBinder getBinder() { |
| 276 | return token.asBinder(); |
| 277 | } |
| 278 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 279 | @Override |
| 280 | public void binderDied() { |
| 281 | ObbAction action = new UnmountObbAction(this, true); |
| 282 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 283 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 284 | |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 285 | public void link() throws RemoteException { |
| 286 | getBinder().linkToDeath(this, 0); |
| 287 | } |
| 288 | |
| 289 | public void unlink() { |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 290 | getBinder().unlinkToDeath(this, 0); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 291 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 292 | |
| 293 | @Override |
| 294 | public String toString() { |
| 295 | StringBuilder sb = new StringBuilder("ObbState{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 296 | sb.append("rawPath=").append(rawPath); |
| 297 | sb.append(",canonicalPath=").append(canonicalPath); |
| 298 | sb.append(",ownerPath=").append(ownerPath); |
| 299 | sb.append(",voldPath=").append(voldPath); |
| 300 | sb.append(",ownerGid=").append(ownerGid); |
| 301 | sb.append(",token=").append(token); |
| 302 | sb.append(",binder=").append(getBinder()); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 303 | sb.append('}'); |
| 304 | return sb.toString(); |
| 305 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // OBB Action Handler |
| 309 | final private ObbActionHandler mObbActionHandler; |
| 310 | |
| 311 | // OBB action handler messages |
| 312 | private static final int OBB_RUN_ACTION = 1; |
| 313 | private static final int OBB_MCS_BOUND = 2; |
| 314 | private static final int OBB_MCS_UNBIND = 3; |
| 315 | private static final int OBB_MCS_RECONNECT = 4; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 316 | private static final int OBB_FLUSH_MOUNT_STATE = 5; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * Default Container Service information |
| 320 | */ |
| 321 | static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName( |
| 322 | "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService"); |
| 323 | |
| 324 | final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection(); |
| 325 | |
| 326 | class DefaultContainerConnection implements ServiceConnection { |
| 327 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 328 | if (DEBUG_OBB) |
| 329 | Slog.i(TAG, "onServiceConnected"); |
| 330 | IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service); |
| 331 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs)); |
| 332 | } |
| 333 | |
| 334 | public void onServiceDisconnected(ComponentName name) { |
| 335 | if (DEBUG_OBB) |
| 336 | Slog.i(TAG, "onServiceDisconnected"); |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | // Used in the ObbActionHandler |
| 341 | private IMediaContainerService mContainerService = null; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 342 | |
| 343 | // Handler messages |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 344 | private static final int H_UNMOUNT_PM_UPDATE = 1; |
| 345 | private static final int H_UNMOUNT_PM_DONE = 2; |
| 346 | private static final int H_UNMOUNT_MS = 3; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 347 | private static final int H_SYSTEM_READY = 4; |
| 348 | |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 349 | private static final int RETRY_UNMOUNT_DELAY = 30; // in ms |
| 350 | private static final int MAX_UNMOUNT_RETRIES = 4; |
| 351 | |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 352 | class UnmountCallBack { |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 353 | final String path; |
| 354 | final boolean force; |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 355 | final boolean removeEncryption; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 356 | int retries; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 357 | |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 358 | UnmountCallBack(String path, boolean force, boolean removeEncryption) { |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 359 | retries = 0; |
| 360 | this.path = path; |
| 361 | this.force = force; |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 362 | this.removeEncryption = removeEncryption; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 363 | } |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 364 | |
| 365 | void handleFinished() { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 366 | if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path); |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 367 | doUnmountVolume(path, true, removeEncryption); |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | class UmsEnableCallBack extends UnmountCallBack { |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 372 | final String method; |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 373 | |
| 374 | UmsEnableCallBack(String path, String method, boolean force) { |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 375 | super(path, force, false); |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 376 | this.method = method; |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | void handleFinished() { |
| 381 | super.handleFinished(); |
| 382 | doShareUnshareVolume(path, method, true); |
| 383 | } |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 386 | class ShutdownCallBack extends UnmountCallBack { |
| 387 | IMountShutdownObserver observer; |
| 388 | ShutdownCallBack(String path, IMountShutdownObserver observer) { |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 389 | super(path, true, false); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 390 | this.observer = observer; |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | void handleFinished() { |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 395 | int ret = doUnmountVolume(path, true, removeEncryption); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 396 | if (observer != null) { |
| 397 | try { |
| 398 | observer.onShutDownComplete(ret); |
| 399 | } catch (RemoteException e) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 400 | Slog.w(TAG, "RemoteException when shutting down"); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 406 | class MountServiceHandler extends Handler { |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 407 | ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>(); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 408 | boolean mUpdatingStatus = false; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 409 | |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 410 | MountServiceHandler(Looper l) { |
| 411 | super(l); |
| 412 | } |
| 413 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 414 | @Override |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 415 | public void handleMessage(Message msg) { |
| 416 | switch (msg.what) { |
| 417 | case H_UNMOUNT_PM_UPDATE: { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 418 | if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE"); |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 419 | UnmountCallBack ucb = (UnmountCallBack) msg.obj; |
| 420 | mForceUnmounts.add(ucb); |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 421 | if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 422 | // Register only if needed. |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 423 | if (!mUpdatingStatus) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 424 | if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager"); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 425 | mUpdatingStatus = true; |
| 426 | mPms.updateExternalMediaStatus(false, true); |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 427 | } |
| 428 | break; |
| 429 | } |
| 430 | case H_UNMOUNT_PM_DONE: { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 431 | if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE"); |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 432 | if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests"); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 433 | mUpdatingStatus = false; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 434 | int size = mForceUnmounts.size(); |
| 435 | int sizeArr[] = new int[size]; |
| 436 | int sizeArrN = 0; |
Suchi Amalapurapu | 7af074a | 2010-04-05 16:46:32 -0700 | [diff] [blame] | 437 | // Kill processes holding references first |
| 438 | ActivityManagerService ams = (ActivityManagerService) |
| 439 | ServiceManager.getService("activity"); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 440 | for (int i = 0; i < size; i++) { |
| 441 | UnmountCallBack ucb = mForceUnmounts.get(i); |
| 442 | String path = ucb.path; |
| 443 | boolean done = false; |
| 444 | if (!ucb.force) { |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 445 | done = true; |
| 446 | } else { |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 447 | int pids[] = getStorageUsers(path); |
| 448 | if (pids == null || pids.length == 0) { |
| 449 | done = true; |
| 450 | } else { |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 451 | // Eliminate system process here? |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 452 | ams.killPids(pids, "unmount media", true); |
Suchi Amalapurapu | 7af074a | 2010-04-05 16:46:32 -0700 | [diff] [blame] | 453 | // Confirm if file references have been freed. |
| 454 | pids = getStorageUsers(path); |
| 455 | if (pids == null || pids.length == 0) { |
| 456 | done = true; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | } |
Suchi Amalapurapu | 7af074a | 2010-04-05 16:46:32 -0700 | [diff] [blame] | 460 | if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) { |
| 461 | // Retry again |
| 462 | Slog.i(TAG, "Retrying to kill storage users again"); |
| 463 | mHandler.sendMessageDelayed( |
| 464 | mHandler.obtainMessage(H_UNMOUNT_PM_DONE, |
| 465 | ucb.retries++), |
| 466 | RETRY_UNMOUNT_DELAY); |
| 467 | } else { |
| 468 | if (ucb.retries >= MAX_UNMOUNT_RETRIES) { |
| 469 | Slog.i(TAG, "Failed to unmount media inspite of " + |
| 470 | MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now"); |
| 471 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 472 | sizeArr[sizeArrN++] = i; |
| 473 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS, |
| 474 | ucb)); |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 475 | } |
| 476 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 477 | // Remove already processed elements from list. |
| 478 | for (int i = (sizeArrN-1); i >= 0; i--) { |
| 479 | mForceUnmounts.remove(sizeArr[i]); |
| 480 | } |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 481 | break; |
| 482 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 483 | case H_UNMOUNT_MS: { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 484 | if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS"); |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 485 | UnmountCallBack ucb = (UnmountCallBack) msg.obj; |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 486 | ucb.handleFinished(); |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 487 | break; |
| 488 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 489 | case H_SYSTEM_READY: { |
| 490 | try { |
| 491 | handleSystemReady(); |
| 492 | } catch (Exception ex) { |
| 493 | Slog.e(TAG, "Boot-time mount exception", ex); |
| 494 | } |
| 495 | break; |
| 496 | } |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | }; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 500 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 501 | private final Handler mHandler; |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 502 | |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 503 | void waitForAsecScan() { |
| 504 | waitForLatch(mAsecsScanned); |
| 505 | } |
| 506 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 507 | private void waitForReady() { |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 508 | waitForLatch(mConnectedSignal); |
| 509 | } |
| 510 | |
| 511 | private void waitForLatch(CountDownLatch latch) { |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 512 | for (;;) { |
| 513 | try { |
| 514 | if (latch.await(5000, TimeUnit.MILLISECONDS)) { |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 515 | return; |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 516 | } else { |
| 517 | Slog.w(TAG, "Thread " + Thread.currentThread().getName() |
| 518 | + " still waiting for MountService ready..."); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 519 | } |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 520 | } catch (InterruptedException e) { |
| 521 | Slog.w(TAG, "Interrupt while waiting for MountService to be ready."); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 522 | } |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 523 | } |
San Mehat | 1f6301e | 2010-01-07 22:40:27 -0800 | [diff] [blame] | 524 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 525 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 526 | private void handleSystemReady() { |
| 527 | // Snapshot current volume states since it's not safe to call into vold |
| 528 | // while holding locks. |
| 529 | final HashMap<String, String> snapshot; |
| 530 | synchronized (mVolumesLock) { |
| 531 | snapshot = new HashMap<String, String>(mVolumeStates); |
| 532 | } |
| 533 | |
| 534 | for (Map.Entry<String, String> entry : snapshot.entrySet()) { |
| 535 | final String path = entry.getKey(); |
| 536 | final String state = entry.getValue(); |
| 537 | |
| 538 | if (state.equals(Environment.MEDIA_UNMOUNTED)) { |
| 539 | int rc = doMountVolume(path); |
| 540 | if (rc != StorageResultCode.OperationSucceeded) { |
| 541 | Slog.e(TAG, String.format("Boot-time mount failed (%d)", |
| 542 | rc)); |
| 543 | } |
| 544 | } else if (state.equals(Environment.MEDIA_SHARED)) { |
| 545 | /* |
| 546 | * Bootstrap UMS enabled state since vold indicates |
| 547 | * the volume is shared (runtime restart while ums enabled) |
| 548 | */ |
| 549 | notifyVolumeStateChange(null, path, VolumeState.NoMedia, |
| 550 | VolumeState.Shared); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | // Push mounted state for all emulated storage |
| 555 | synchronized (mVolumesLock) { |
| 556 | for (StorageVolume volume : mVolumes) { |
| 557 | if (volume.isEmulated()) { |
| 558 | updatePublicVolumeState(volume, Environment.MEDIA_MOUNTED); |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * If UMS was connected on boot, send the connected event |
| 565 | * now that we're up. |
| 566 | */ |
| 567 | if (mSendUmsConnectedOnBoot) { |
| 568 | sendUmsIntent(true); |
| 569 | mSendUmsConnectedOnBoot = false; |
| 570 | } |
| 571 | } |
| 572 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 573 | private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
| 574 | @Override |
| 575 | public void onReceive(Context context, Intent intent) { |
| 576 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 577 | if (userId == -1) return; |
| 578 | final UserHandle user = new UserHandle(userId); |
| 579 | |
| 580 | final String action = intent.getAction(); |
| 581 | if (Intent.ACTION_USER_ADDED.equals(action)) { |
| 582 | synchronized (mVolumesLock) { |
| 583 | createEmulatedVolumeForUserLocked(user); |
| 584 | } |
| 585 | |
| 586 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 587 | synchronized (mVolumesLock) { |
| 588 | final List<StorageVolume> toRemove = Lists.newArrayList(); |
| 589 | for (StorageVolume volume : mVolumes) { |
| 590 | if (user.equals(volume.getOwner())) { |
| 591 | toRemove.add(volume); |
| 592 | } |
| 593 | } |
| 594 | for (StorageVolume volume : toRemove) { |
| 595 | removeVolumeLocked(volume); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | }; |
| 601 | |
| 602 | private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { |
| 603 | @Override |
| 604 | public void onReceive(Context context, Intent intent) { |
| 605 | boolean available = (intent.getBooleanExtra(UsbManager.USB_CONNECTED, false) && |
| 606 | intent.getBooleanExtra(UsbManager.USB_FUNCTION_MASS_STORAGE, false)); |
| 607 | notifyShareAvailabilityChange(available); |
| 608 | } |
| 609 | }; |
| 610 | |
Svetoslav | a7452ee | 2013-03-20 12:06:59 -0700 | [diff] [blame] | 611 | private final BroadcastReceiver mIdleMaintenanceReceiver = new BroadcastReceiver() { |
| 612 | @Override |
| 613 | public void onReceive(Context context, Intent intent) { |
| 614 | waitForReady(); |
| 615 | String action = intent.getAction(); |
| 616 | // Since fstrim will be run on a daily basis we do not expect |
| 617 | // fstrim to be too long, so it is not interruptible. We will |
| 618 | // implement interruption only in case we see issues. |
| 619 | if (Intent.ACTION_IDLE_MAINTENANCE_START.equals(action)) { |
| 620 | try { |
| 621 | // This method runs on the handler thread, |
| 622 | // so it is safe to directly call into vold. |
| 623 | mConnector.execute("fstrim", "dotrim"); |
Svetoslav | f23b64d | 2013-04-25 14:45:54 -0700 | [diff] [blame] | 624 | EventLogTags.writeFstrimStart(SystemClock.elapsedRealtime()); |
Svetoslav | a7452ee | 2013-03-20 12:06:59 -0700 | [diff] [blame] | 625 | } catch (NativeDaemonConnectorException ndce) { |
| 626 | Slog.e(TAG, "Failed to run fstrim!"); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | }; |
| 631 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 632 | private final class MountServiceBinderListener implements IBinder.DeathRecipient { |
| 633 | final IMountServiceListener mListener; |
| 634 | |
| 635 | MountServiceBinderListener(IMountServiceListener listener) { |
| 636 | mListener = listener; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 637 | |
San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 638 | } |
| 639 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 640 | public void binderDied() { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 641 | if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!"); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 642 | synchronized (mListeners) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 643 | mListeners.remove(this); |
| 644 | mListener.asBinder().unlinkToDeath(this, 0); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 649 | private void doShareUnshareVolume(String path, String method, boolean enable) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 650 | // TODO: Add support for multiple share methods |
| 651 | if (!method.equals("ums")) { |
| 652 | throw new IllegalArgumentException(String.format("Method %s not supported", method)); |
| 653 | } |
| 654 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 655 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 656 | mConnector.execute("volume", enable ? "share" : "unshare", path, method); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 657 | } catch (NativeDaemonConnectorException e) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 658 | Slog.e(TAG, "Failed to share/unshare", e); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 659 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 660 | } |
| 661 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 662 | private void updatePublicVolumeState(StorageVolume volume, String state) { |
| 663 | final String path = volume.getPath(); |
| 664 | final String oldState; |
| 665 | synchronized (mVolumesLock) { |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 666 | oldState = mVolumeStates.put(path, state); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 667 | volume.setState(state); |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 668 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 669 | |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 670 | if (state.equals(oldState)) { |
| 671 | Slog.w(TAG, String.format("Duplicate state transition (%s -> %s) for %s", |
| 672 | state, state, path)); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 673 | return; |
| 674 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 675 | |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 676 | Slog.d(TAG, "volume state changed for " + path + " (" + oldState + " -> " + state + ")"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 677 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 678 | // Tell PackageManager about changes to primary volume state, but only |
| 679 | // when not emulated. |
| 680 | if (volume.isPrimary() && !volume.isEmulated()) { |
| 681 | if (Environment.MEDIA_UNMOUNTED.equals(state)) { |
| 682 | mPms.updateExternalMediaStatus(false, false); |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 683 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 684 | /* |
| 685 | * Some OBBs might have been unmounted when this volume was |
| 686 | * unmounted, so send a message to the handler to let it know to |
| 687 | * remove those from the list of mounted OBBS. |
| 688 | */ |
| 689 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage( |
| 690 | OBB_FLUSH_MOUNT_STATE, path)); |
| 691 | } else if (Environment.MEDIA_MOUNTED.equals(state)) { |
| 692 | mPms.updateExternalMediaStatus(true, false); |
Mike Lockwood | 0355975 | 2010-07-19 18:25:03 -0400 | [diff] [blame] | 693 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 694 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 695 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 696 | synchronized (mListeners) { |
| 697 | for (int i = mListeners.size() -1; i >= 0; i--) { |
| 698 | MountServiceBinderListener bl = mListeners.get(i); |
| 699 | try { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 700 | bl.mListener.onStorageStateChanged(path, oldState, state); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 701 | } catch (RemoteException rex) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 702 | Slog.e(TAG, "Listener dead"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 703 | mListeners.remove(i); |
| 704 | } catch (Exception ex) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 705 | Slog.e(TAG, "Listener failed", ex); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 712 | * Callback from NativeDaemonConnector |
| 713 | */ |
| 714 | public void onDaemonConnected() { |
| 715 | /* |
| 716 | * Since we'll be calling back into the NativeDaemonConnector, |
| 717 | * we need to do our work in a new thread. |
| 718 | */ |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 719 | new Thread("MountService#onDaemonConnected") { |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 720 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 721 | public void run() { |
| 722 | /** |
| 723 | * Determine media state and UMS detection status |
| 724 | */ |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 725 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 726 | final String[] vols = NativeDaemonEvent.filterMessageList( |
| 727 | mConnector.executeForList("volume", "list"), |
| 728 | VoldResponseCode.VolumeListResult); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 729 | for (String volstr : vols) { |
| 730 | String[] tok = volstr.split(" "); |
| 731 | // FMT: <label> <mountpoint> <state> |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 732 | String path = tok[1]; |
| 733 | String state = Environment.MEDIA_REMOVED; |
| 734 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 735 | final StorageVolume volume; |
| 736 | synchronized (mVolumesLock) { |
| 737 | volume = mVolumesByPath.get(path); |
| 738 | } |
| 739 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 740 | int st = Integer.parseInt(tok[2]); |
| 741 | if (st == VolumeState.NoMedia) { |
| 742 | state = Environment.MEDIA_REMOVED; |
| 743 | } else if (st == VolumeState.Idle) { |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 744 | state = Environment.MEDIA_UNMOUNTED; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 745 | } else if (st == VolumeState.Mounted) { |
| 746 | state = Environment.MEDIA_MOUNTED; |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 747 | Slog.i(TAG, "Media already mounted on daemon connection"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 748 | } else if (st == VolumeState.Shared) { |
| 749 | state = Environment.MEDIA_SHARED; |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 750 | Slog.i(TAG, "Media shared on daemon connection"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 751 | } else { |
| 752 | throw new Exception(String.format("Unexpected state %d", st)); |
| 753 | } |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 754 | |
| 755 | if (state != null) { |
| 756 | if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 757 | updatePublicVolumeState(volume, state); |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 758 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 759 | } |
| 760 | } catch (Exception e) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 761 | Slog.e(TAG, "Error processing initial volume state", e); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 762 | final StorageVolume primary = getPrimaryPhysicalVolume(); |
| 763 | if (primary != null) { |
| 764 | updatePublicVolumeState(primary, Environment.MEDIA_REMOVED); |
| 765 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 766 | } |
| 767 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 768 | /* |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 769 | * Now that we've done our initialization, release |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 770 | * the hounds! |
| 771 | */ |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 772 | mConnectedSignal.countDown(); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 773 | |
| 774 | // Let package manager load internal ASECs. |
| 775 | mPms.scanAvailableAsecs(); |
| 776 | |
| 777 | // Notify people waiting for ASECs to be scanned that it's done. |
| 778 | mAsecsScanned.countDown(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 779 | } |
| 780 | }.start(); |
| 781 | } |
| 782 | |
| 783 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 784 | * Callback from NativeDaemonConnector |
| 785 | */ |
| 786 | public boolean onEvent(int code, String raw, String[] cooked) { |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 787 | if (DEBUG_EVENTS) { |
| 788 | StringBuilder builder = new StringBuilder(); |
| 789 | builder.append("onEvent::"); |
| 790 | builder.append(" raw= " + raw); |
| 791 | if (cooked != null) { |
| 792 | builder.append(" cooked = " ); |
| 793 | for (String str : cooked) { |
| 794 | builder.append(" " + str); |
| 795 | } |
| 796 | } |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 797 | Slog.i(TAG, builder.toString()); |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 798 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 799 | if (code == VoldResponseCode.VolumeStateChange) { |
| 800 | /* |
| 801 | * One of the volumes we're managing has changed state. |
| 802 | * Format: "NNN Volume <label> <path> state changed |
| 803 | * from <old_#> (<old_str>) to <new_#> (<new_str>)" |
| 804 | */ |
| 805 | notifyVolumeStateChange( |
| 806 | cooked[2], cooked[3], Integer.parseInt(cooked[7]), |
| 807 | Integer.parseInt(cooked[10])); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 808 | } else if (code == VoldResponseCode.VolumeUuidChange) { |
| 809 | // Format: nnn <label> <path> <uuid> |
| 810 | final String path = cooked[2]; |
| 811 | final String uuid = (cooked.length > 3) ? cooked[3] : null; |
| 812 | |
| 813 | final StorageVolume vol = mVolumesByPath.get(path); |
| 814 | if (vol != null) { |
| 815 | vol.setUuid(uuid); |
| 816 | } |
| 817 | |
| 818 | } else if (code == VoldResponseCode.VolumeUserLabelChange) { |
| 819 | // Format: nnn <label> <path> <label> |
| 820 | final String path = cooked[2]; |
| 821 | final String userLabel = (cooked.length > 3) ? cooked[3] : null; |
| 822 | |
| 823 | final StorageVolume vol = mVolumesByPath.get(path); |
| 824 | if (vol != null) { |
| 825 | vol.setUserLabel(userLabel); |
| 826 | } |
| 827 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 828 | } else if ((code == VoldResponseCode.VolumeDiskInserted) || |
| 829 | (code == VoldResponseCode.VolumeDiskRemoved) || |
| 830 | (code == VoldResponseCode.VolumeBadRemoval)) { |
| 831 | // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>) |
| 832 | // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>) |
| 833 | // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>) |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 834 | String action = null; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 835 | final String label = cooked[2]; |
| 836 | final String path = cooked[3]; |
| 837 | int major = -1; |
| 838 | int minor = -1; |
| 839 | |
| 840 | try { |
| 841 | String devComp = cooked[6].substring(1, cooked[6].length() -1); |
| 842 | String[] devTok = devComp.split(":"); |
| 843 | major = Integer.parseInt(devTok[0]); |
| 844 | minor = Integer.parseInt(devTok[1]); |
| 845 | } catch (Exception ex) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 846 | Slog.e(TAG, "Failed to parse major/minor", ex); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 847 | } |
| 848 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 849 | final StorageVolume volume; |
| 850 | final String state; |
| 851 | synchronized (mVolumesLock) { |
| 852 | volume = mVolumesByPath.get(path); |
| 853 | state = mVolumeStates.get(path); |
| 854 | } |
| 855 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 856 | if (code == VoldResponseCode.VolumeDiskInserted) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 857 | new Thread("MountService#VolumeDiskInserted") { |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 858 | @Override |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 859 | public void run() { |
| 860 | try { |
| 861 | int rc; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 862 | if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 863 | Slog.w(TAG, String.format("Insertion mount failed (%d)", rc)); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 864 | } |
| 865 | } catch (Exception ex) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 866 | Slog.w(TAG, "Failed to mount media on insertion", ex); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | }.start(); |
| 870 | } else if (code == VoldResponseCode.VolumeDiskRemoved) { |
| 871 | /* |
| 872 | * This event gets trumped if we're already in BAD_REMOVAL state |
| 873 | */ |
| 874 | if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) { |
| 875 | return true; |
| 876 | } |
| 877 | /* Send the media unmounted event first */ |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 878 | if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 879 | updatePublicVolumeState(volume, Environment.MEDIA_UNMOUNTED); |
Johan Redestig | 0464c07 | 2014-01-18 22:46:56 +0100 | [diff] [blame] | 880 | sendStorageIntent(Intent.ACTION_MEDIA_UNMOUNTED, volume, UserHandle.ALL); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 881 | |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 882 | if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 883 | updatePublicVolumeState(volume, Environment.MEDIA_REMOVED); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 884 | action = Intent.ACTION_MEDIA_REMOVED; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 885 | } else if (code == VoldResponseCode.VolumeBadRemoval) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 886 | if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 887 | /* Send the media unmounted event first */ |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 888 | updatePublicVolumeState(volume, Environment.MEDIA_UNMOUNTED); |
Christian Beck | f503c8f | 2013-05-20 08:42:45 +0200 | [diff] [blame] | 889 | sendStorageIntent(Intent.ACTION_MEDIA_UNMOUNTED, volume, UserHandle.ALL); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 890 | |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 891 | if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 892 | updatePublicVolumeState(volume, Environment.MEDIA_BAD_REMOVAL); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 893 | action = Intent.ACTION_MEDIA_BAD_REMOVAL; |
Svetoslav | f23b64d | 2013-04-25 14:45:54 -0700 | [diff] [blame] | 894 | } else if (code == VoldResponseCode.FstrimCompleted) { |
Svetoslav | 9e814a8 | 2013-04-30 10:43:56 -0700 | [diff] [blame] | 895 | EventLogTags.writeFstrimFinish(SystemClock.elapsedRealtime()); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 896 | } else { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 897 | Slog.e(TAG, String.format("Unknown code {%d}", code)); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 898 | } |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 899 | |
| 900 | if (action != null) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 901 | sendStorageIntent(action, volume, UserHandle.ALL); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 902 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 903 | } else { |
| 904 | return false; |
| 905 | } |
| 906 | |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 907 | return true; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 908 | } |
| 909 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 910 | private void notifyVolumeStateChange(String label, String path, int oldState, int newState) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 911 | final StorageVolume volume; |
| 912 | final String state; |
| 913 | synchronized (mVolumesLock) { |
| 914 | volume = mVolumesByPath.get(path); |
| 915 | state = getVolumeState(path); |
| 916 | } |
| 917 | |
| 918 | if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChange::" + state); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 919 | |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 920 | String action = null; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 921 | |
Mike Lockwood | bf2dd44 | 2010-03-03 06:16:52 -0500 | [diff] [blame] | 922 | if (oldState == VolumeState.Shared && newState != oldState) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 923 | if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 924 | sendStorageIntent(Intent.ACTION_MEDIA_UNSHARED, volume, UserHandle.ALL); |
Mike Lockwood | bf2dd44 | 2010-03-03 06:16:52 -0500 | [diff] [blame] | 925 | } |
| 926 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 927 | if (newState == VolumeState.Init) { |
| 928 | } else if (newState == VolumeState.NoMedia) { |
| 929 | // NoMedia is handled via Disk Remove events |
| 930 | } else if (newState == VolumeState.Idle) { |
| 931 | /* |
| 932 | * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or |
| 933 | * if we're in the process of enabling UMS |
| 934 | */ |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 935 | if (!state.equals( |
| 936 | Environment.MEDIA_BAD_REMOVAL) && !state.equals( |
| 937 | Environment.MEDIA_NOFS) && !state.equals( |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 938 | Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 939 | if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 940 | updatePublicVolumeState(volume, Environment.MEDIA_UNMOUNTED); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 941 | action = Intent.ACTION_MEDIA_UNMOUNTED; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 942 | } |
| 943 | } else if (newState == VolumeState.Pending) { |
| 944 | } else if (newState == VolumeState.Checking) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 945 | if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 946 | updatePublicVolumeState(volume, Environment.MEDIA_CHECKING); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 947 | action = Intent.ACTION_MEDIA_CHECKING; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 948 | } else if (newState == VolumeState.Mounted) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 949 | if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 950 | updatePublicVolumeState(volume, Environment.MEDIA_MOUNTED); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 951 | action = Intent.ACTION_MEDIA_MOUNTED; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 952 | } else if (newState == VolumeState.Unmounting) { |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 953 | action = Intent.ACTION_MEDIA_EJECT; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 954 | } else if (newState == VolumeState.Formatting) { |
| 955 | } else if (newState == VolumeState.Shared) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 956 | if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 957 | /* Send the media unmounted event first */ |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 958 | updatePublicVolumeState(volume, Environment.MEDIA_UNMOUNTED); |
| 959 | sendStorageIntent(Intent.ACTION_MEDIA_UNMOUNTED, volume, UserHandle.ALL); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 960 | |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 961 | if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 962 | updatePublicVolumeState(volume, Environment.MEDIA_SHARED); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 963 | action = Intent.ACTION_MEDIA_SHARED; |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 964 | if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 965 | } else if (newState == VolumeState.SharedMnt) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 966 | Slog.e(TAG, "Live shared mounts not supported yet!"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 967 | return; |
| 968 | } else { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 969 | Slog.e(TAG, "Unhandled VolumeState {" + newState + "}"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 970 | } |
| 971 | |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 972 | if (action != null) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 973 | sendStorageIntent(action, volume, UserHandle.ALL); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 977 | private int doMountVolume(String path) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 978 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 979 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 980 | final StorageVolume volume; |
| 981 | synchronized (mVolumesLock) { |
| 982 | volume = mVolumesByPath.get(path); |
| 983 | } |
| 984 | |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 985 | if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 986 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 987 | mConnector.execute("volume", "mount", path); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 988 | } catch (NativeDaemonConnectorException e) { |
| 989 | /* |
| 990 | * Mount failed for some reason |
| 991 | */ |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 992 | String action = null; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 993 | int code = e.getCode(); |
| 994 | if (code == VoldResponseCode.OpFailedNoMedia) { |
| 995 | /* |
| 996 | * Attempt to mount but no media inserted |
| 997 | */ |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 998 | rc = StorageResultCode.OperationFailedNoMedia; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 999 | } else if (code == VoldResponseCode.OpFailedMediaBlank) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1000 | if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs"); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1001 | /* |
| 1002 | * Media is blank or does not contain a supported filesystem |
| 1003 | */ |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1004 | updatePublicVolumeState(volume, Environment.MEDIA_NOFS); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 1005 | action = Intent.ACTION_MEDIA_NOFS; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1006 | rc = StorageResultCode.OperationFailedMediaBlank; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1007 | } else if (code == VoldResponseCode.OpFailedMediaCorrupt) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1008 | if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt"); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1009 | /* |
| 1010 | * Volume consistency check failed |
| 1011 | */ |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1012 | updatePublicVolumeState(volume, Environment.MEDIA_UNMOUNTABLE); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 1013 | action = Intent.ACTION_MEDIA_UNMOUNTABLE; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1014 | rc = StorageResultCode.OperationFailedMediaCorrupt; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1015 | } else { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1016 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * Send broadcast intent (if required for the failure) |
| 1021 | */ |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 1022 | if (action != null) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1023 | sendStorageIntent(action, volume, UserHandle.ALL); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | return rc; |
| 1028 | } |
| 1029 | |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 1030 | /* |
| 1031 | * If force is not set, we do not unmount if there are |
| 1032 | * processes holding references to the volume about to be unmounted. |
| 1033 | * If force is set, all the processes holding references need to be |
| 1034 | * killed via the ActivityManager before actually unmounting the volume. |
| 1035 | * This might even take a while and might be retried after timed delays |
| 1036 | * to make sure we dont end up in an instable state and kill some core |
| 1037 | * processes. |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 1038 | * If removeEncryption is set, force is implied, and the system will remove any encryption |
| 1039 | * mapping set on the volume when unmounting. |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 1040 | */ |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 1041 | private int doUnmountVolume(String path, boolean force, boolean removeEncryption) { |
San Mehat | 59443a6 | 2010-02-09 13:28:45 -0800 | [diff] [blame] | 1042 | if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) { |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1043 | return VoldResponseCode.OpFailedVolNotMounted; |
| 1044 | } |
Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 1045 | |
| 1046 | /* |
| 1047 | * Force a GC to make sure AssetManagers in other threads of the |
| 1048 | * system_server are cleaned up. We have to do this since AssetManager |
| 1049 | * instances are kept as a WeakReference and it's possible we have files |
| 1050 | * open on the external storage. |
| 1051 | */ |
| 1052 | Runtime.getRuntime().gc(); |
| 1053 | |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 1054 | // Redundant probably. But no harm in updating state again. |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 1055 | mPms.updateExternalMediaStatus(false, false); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1056 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1057 | final Command cmd = new Command("volume", "unmount", path); |
| 1058 | if (removeEncryption) { |
| 1059 | cmd.appendArg("force_and_revert"); |
| 1060 | } else if (force) { |
| 1061 | cmd.appendArg("force"); |
| 1062 | } |
| 1063 | mConnector.execute(cmd); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 1064 | // We unmounted the volume. None of the asec containers are available now. |
| 1065 | synchronized (mAsecMountSet) { |
| 1066 | mAsecMountSet.clear(); |
| 1067 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1068 | return StorageResultCode.OperationSucceeded; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1069 | } catch (NativeDaemonConnectorException e) { |
| 1070 | // Don't worry about mismatch in PackageManager since the |
| 1071 | // call back will handle the status changes any way. |
| 1072 | int code = e.getCode(); |
| 1073 | if (code == VoldResponseCode.OpFailedVolNotMounted) { |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1074 | return StorageResultCode.OperationFailedStorageNotMounted; |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1075 | } else if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1076 | return StorageResultCode.OperationFailedStorageBusy; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1077 | } else { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1078 | return StorageResultCode.OperationFailedInternalError; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | private int doFormatVolume(String path) { |
| 1084 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1085 | mConnector.execute("volume", "format", path); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1086 | return StorageResultCode.OperationSucceeded; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1087 | } catch (NativeDaemonConnectorException e) { |
| 1088 | int code = e.getCode(); |
| 1089 | if (code == VoldResponseCode.OpFailedNoMedia) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1090 | return StorageResultCode.OperationFailedNoMedia; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1091 | } else if (code == VoldResponseCode.OpFailedMediaCorrupt) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1092 | return StorageResultCode.OperationFailedMediaCorrupt; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1093 | } else { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1094 | return StorageResultCode.OperationFailedInternalError; |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1099 | private boolean doGetVolumeShared(String path, String method) { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1100 | final NativeDaemonEvent event; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1101 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1102 | event = mConnector.execute("volume", "shared", path, method); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 1103 | } catch (NativeDaemonConnectorException ex) { |
| 1104 | Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method); |
| 1105 | return false; |
| 1106 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1107 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1108 | if (event.getCode() == VoldResponseCode.ShareEnabledResult) { |
| 1109 | return event.getMessage().endsWith("enabled"); |
| 1110 | } else { |
| 1111 | return false; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1112 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Mike Lockwood | ecedfdc | 2011-06-08 15:11:59 -0700 | [diff] [blame] | 1115 | private void notifyShareAvailabilityChange(final boolean avail) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1116 | synchronized (mListeners) { |
Mike Lockwood | ecedfdc | 2011-06-08 15:11:59 -0700 | [diff] [blame] | 1117 | mUmsAvailable = avail; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1118 | for (int i = mListeners.size() -1; i >= 0; i--) { |
| 1119 | MountServiceBinderListener bl = mListeners.get(i); |
| 1120 | try { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1121 | bl.mListener.onUsbMassStorageConnectionChanged(avail); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1122 | } catch (RemoteException rex) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1123 | Slog.e(TAG, "Listener dead"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1124 | mListeners.remove(i); |
| 1125 | } catch (Exception ex) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1126 | Slog.e(TAG, "Listener failed", ex); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1131 | if (mSystemReady == true) { |
San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 1132 | sendUmsIntent(avail); |
| 1133 | } else { |
| 1134 | mSendUmsConnectedOnBoot = avail; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1135 | } |
San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 1136 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1137 | final StorageVolume primary = getPrimaryPhysicalVolume(); |
| 1138 | if (avail == false && primary != null |
| 1139 | && Environment.MEDIA_SHARED.equals(getVolumeState(primary.getPath()))) { |
| 1140 | final String path = primary.getPath(); |
San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 1141 | /* |
| 1142 | * USB mass storage disconnected while enabled |
| 1143 | */ |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 1144 | new Thread("MountService#AvailabilityChange") { |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 1145 | @Override |
San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 1146 | public void run() { |
| 1147 | try { |
| 1148 | int rc; |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1149 | Slog.w(TAG, "Disabling UMS after cable disconnect"); |
San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 1150 | doShareUnshareVolume(path, "ums", false); |
| 1151 | if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1152 | Slog.e(TAG, String.format( |
San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 1153 | "Failed to remount {%s} on UMS enabled-disconnect (%d)", |
| 1154 | path, rc)); |
| 1155 | } |
| 1156 | } catch (Exception ex) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1157 | Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex); |
San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | }.start(); |
| 1161 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1164 | private void sendStorageIntent(String action, StorageVolume volume, UserHandle user) { |
| 1165 | final Intent intent = new Intent(action, Uri.parse("file://" + volume.getPath())); |
| 1166 | intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, volume); |
Johan Redestig | 0464c07 | 2014-01-18 22:46:56 +0100 | [diff] [blame] | 1167 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1168 | Slog.d(TAG, "sendStorageIntent " + intent + " to " + user); |
| 1169 | mContext.sendBroadcastAsUser(intent, user); |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 1170 | } |
| 1171 | |
San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 1172 | private void sendUmsIntent(boolean c) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1173 | mContext.sendBroadcastAsUser( |
| 1174 | new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)), |
| 1175 | UserHandle.ALL); |
San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 1176 | } |
| 1177 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1178 | private void validatePermission(String perm) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1179 | if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) { |
| 1180 | throw new SecurityException(String.format("Requires %s permission", perm)); |
| 1181 | } |
| 1182 | } |
| 1183 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1184 | // Storage list XML tags |
| 1185 | private static final String TAG_STORAGE_LIST = "StorageList"; |
| 1186 | private static final String TAG_STORAGE = "storage"; |
| 1187 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1188 | private void readStorageListLocked() { |
| 1189 | mVolumes.clear(); |
| 1190 | mVolumeStates.clear(); |
| 1191 | |
Fabrice Di Meglio | 13fe2a5 | 2012-05-18 18:08:58 -0700 | [diff] [blame] | 1192 | Resources resources = mContext.getResources(); |
| 1193 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1194 | int id = com.android.internal.R.xml.storage_list; |
| 1195 | XmlResourceParser parser = resources.getXml(id); |
| 1196 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 1197 | |
| 1198 | try { |
| 1199 | XmlUtils.beginDocument(parser, TAG_STORAGE_LIST); |
| 1200 | while (true) { |
| 1201 | XmlUtils.nextElement(parser); |
| 1202 | |
| 1203 | String element = parser.getName(); |
| 1204 | if (element == null) break; |
| 1205 | |
| 1206 | if (TAG_STORAGE.equals(element)) { |
| 1207 | TypedArray a = resources.obtainAttributes(attrs, |
| 1208 | com.android.internal.R.styleable.Storage); |
| 1209 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1210 | String path = a.getString( |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1211 | com.android.internal.R.styleable.Storage_mountPoint); |
Fabrice Di Meglio | 13fe2a5 | 2012-05-18 18:08:58 -0700 | [diff] [blame] | 1212 | int descriptionId = a.getResourceId( |
| 1213 | com.android.internal.R.styleable.Storage_storageDescription, -1); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1214 | CharSequence description = a.getText( |
| 1215 | com.android.internal.R.styleable.Storage_storageDescription); |
| 1216 | boolean primary = a.getBoolean( |
| 1217 | com.android.internal.R.styleable.Storage_primary, false); |
| 1218 | boolean removable = a.getBoolean( |
| 1219 | com.android.internal.R.styleable.Storage_removable, false); |
| 1220 | boolean emulated = a.getBoolean( |
| 1221 | com.android.internal.R.styleable.Storage_emulated, false); |
| 1222 | int mtpReserve = a.getInt( |
| 1223 | com.android.internal.R.styleable.Storage_mtpReserve, 0); |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 1224 | boolean allowMassStorage = a.getBoolean( |
| 1225 | com.android.internal.R.styleable.Storage_allowMassStorage, false); |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 1226 | // resource parser does not support longs, so XML value is in megabytes |
| 1227 | long maxFileSize = a.getInt( |
| 1228 | com.android.internal.R.styleable.Storage_maxFileSize, 0) * 1024L * 1024L; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1229 | |
| 1230 | Slog.d(TAG, "got storage path: " + path + " description: " + description + |
| 1231 | " primary: " + primary + " removable: " + removable + |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 1232 | " emulated: " + emulated + " mtpReserve: " + mtpReserve + |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 1233 | " allowMassStorage: " + allowMassStorage + |
| 1234 | " maxFileSize: " + maxFileSize); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1235 | |
| 1236 | if (emulated) { |
| 1237 | // For devices with emulated storage, we create separate |
| 1238 | // volumes for each known user. |
| 1239 | mEmulatedTemplate = new StorageVolume(null, descriptionId, true, false, |
| 1240 | true, mtpReserve, false, maxFileSize, null); |
| 1241 | |
| 1242 | final UserManagerService userManager = UserManagerService.getInstance(); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1243 | for (UserInfo user : userManager.getUsers(false)) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1244 | createEmulatedVolumeForUserLocked(user.getUserHandle()); |
| 1245 | } |
| 1246 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1247 | } else { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1248 | if (path == null || description == null) { |
| 1249 | Slog.e(TAG, "Missing storage path or description in readStorageList"); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1250 | } else { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1251 | final StorageVolume volume = new StorageVolume(new File(path), |
| 1252 | descriptionId, primary, removable, emulated, mtpReserve, |
| 1253 | allowMassStorage, maxFileSize, null); |
| 1254 | addVolumeLocked(volume); |
Jeff Sharkey | 44cbdec | 2013-10-07 16:49:47 -0700 | [diff] [blame] | 1255 | |
| 1256 | // Until we hear otherwise, treat as unmounted |
| 1257 | mVolumeStates.put(volume.getPath(), Environment.MEDIA_UNMOUNTED); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 1258 | volume.setState(Environment.MEDIA_UNMOUNTED); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1259 | } |
| 1260 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1261 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1262 | a.recycle(); |
| 1263 | } |
| 1264 | } |
| 1265 | } catch (XmlPullParserException e) { |
| 1266 | throw new RuntimeException(e); |
| 1267 | } catch (IOException e) { |
| 1268 | throw new RuntimeException(e); |
| 1269 | } finally { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1270 | // Compute storage ID for each physical volume; emulated storage is |
| 1271 | // always 0 when defined. |
| 1272 | int index = isExternalStorageEmulated() ? 1 : 0; |
| 1273 | for (StorageVolume volume : mVolumes) { |
| 1274 | if (!volume.isEmulated()) { |
| 1275 | volume.setStorageId(index++); |
| 1276 | } |
Mike Lockwood | fbfe555 | 2011-05-17 17:19:37 -0400 | [diff] [blame] | 1277 | } |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1278 | parser.close(); |
| 1279 | } |
| 1280 | } |
| 1281 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1282 | /** |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1283 | * Create and add new {@link StorageVolume} for given {@link UserHandle} |
| 1284 | * using {@link #mEmulatedTemplate} as template. |
| 1285 | */ |
| 1286 | private void createEmulatedVolumeForUserLocked(UserHandle user) { |
| 1287 | if (mEmulatedTemplate == null) { |
| 1288 | throw new IllegalStateException("Missing emulated volume multi-user template"); |
| 1289 | } |
| 1290 | |
| 1291 | final UserEnvironment userEnv = new UserEnvironment(user.getIdentifier()); |
| 1292 | final File path = userEnv.getExternalStorageDirectory(); |
| 1293 | final StorageVolume volume = StorageVolume.fromTemplate(mEmulatedTemplate, path, user); |
| 1294 | volume.setStorageId(0); |
| 1295 | addVolumeLocked(volume); |
| 1296 | |
| 1297 | if (mSystemReady) { |
| 1298 | updatePublicVolumeState(volume, Environment.MEDIA_MOUNTED); |
| 1299 | } else { |
| 1300 | // Place stub status for early callers to find |
| 1301 | mVolumeStates.put(volume.getPath(), Environment.MEDIA_MOUNTED); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 1302 | volume.setState(Environment.MEDIA_MOUNTED); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | private void addVolumeLocked(StorageVolume volume) { |
| 1307 | Slog.d(TAG, "addVolumeLocked() " + volume); |
| 1308 | mVolumes.add(volume); |
| 1309 | final StorageVolume existing = mVolumesByPath.put(volume.getPath(), volume); |
| 1310 | if (existing != null) { |
| 1311 | throw new IllegalStateException( |
| 1312 | "Volume at " + volume.getPath() + " already exists: " + existing); |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | private void removeVolumeLocked(StorageVolume volume) { |
| 1317 | Slog.d(TAG, "removeVolumeLocked() " + volume); |
| 1318 | mVolumes.remove(volume); |
| 1319 | mVolumesByPath.remove(volume.getPath()); |
| 1320 | mVolumeStates.remove(volume.getPath()); |
| 1321 | } |
| 1322 | |
| 1323 | private StorageVolume getPrimaryPhysicalVolume() { |
| 1324 | synchronized (mVolumesLock) { |
| 1325 | for (StorageVolume volume : mVolumes) { |
| 1326 | if (volume.isPrimary() && !volume.isEmulated()) { |
| 1327 | return volume; |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | return null; |
| 1332 | } |
| 1333 | |
| 1334 | /** |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1335 | * Constructs a new MountService instance |
| 1336 | * |
| 1337 | * @param context Binder context for this service |
| 1338 | */ |
| 1339 | public MountService(Context context) { |
| 1340 | mContext = context; |
| 1341 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1342 | synchronized (mVolumesLock) { |
| 1343 | readStorageListLocked(); |
Mike Lockwood | 0355975 | 2010-07-19 18:25:03 -0400 | [diff] [blame] | 1344 | } |
| 1345 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1346 | // XXX: This will go away soon in favor of IMountServiceObserver |
| 1347 | mPms = (PackageManagerService) ServiceManager.getService("package"); |
| 1348 | |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 1349 | HandlerThread hthread = new HandlerThread(TAG); |
| 1350 | hthread.start(); |
| 1351 | mHandler = new MountServiceHandler(hthread.getLooper()); |
Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 1352 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1353 | // Watch for user changes |
| 1354 | final IntentFilter userFilter = new IntentFilter(); |
| 1355 | userFilter.addAction(Intent.ACTION_USER_ADDED); |
| 1356 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 1357 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 1358 | |
| 1359 | // Watch for USB changes on primary volume |
| 1360 | final StorageVolume primary = getPrimaryPhysicalVolume(); |
| 1361 | if (primary != null && primary.allowMassStorage()) { |
| 1362 | mContext.registerReceiver( |
| 1363 | mUsbReceiver, new IntentFilter(UsbManager.ACTION_USB_STATE), null, mHandler); |
| 1364 | } |
| 1365 | |
Svetoslav | a7452ee | 2013-03-20 12:06:59 -0700 | [diff] [blame] | 1366 | // Watch for idle maintenance changes |
| 1367 | IntentFilter idleMaintenanceFilter = new IntentFilter(); |
| 1368 | idleMaintenanceFilter.addAction(Intent.ACTION_IDLE_MAINTENANCE_START); |
| 1369 | mContext.registerReceiverAsUser(mIdleMaintenanceReceiver, UserHandle.ALL, |
| 1370 | idleMaintenanceFilter, null, mHandler); |
| 1371 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1372 | // Add OBB Action Handler to MountService thread. |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 1373 | mObbActionHandler = new ObbActionHandler(IoThread.get().getLooper()); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1374 | |
Marco Nelissen | c34ebce | 2010-02-18 13:39:41 -0800 | [diff] [blame] | 1375 | /* |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 1376 | * Create the connection to vold with a maximum queue of twice the |
| 1377 | * amount of containers we'd ever expect to have. This keeps an |
| 1378 | * "asec list" from blocking a thread repeatedly. |
| 1379 | */ |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 1380 | mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG, 25); |
Kenny Root | 51a573c | 2012-05-17 13:30:28 -0700 | [diff] [blame] | 1381 | |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 1382 | Thread thread = new Thread(mConnector, VOLD_TAG); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1383 | thread.start(); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 1384 | |
Kenny Root | 07714d4 | 2011-08-17 17:49:28 -0700 | [diff] [blame] | 1385 | // Add ourself to the Watchdog monitors if enabled. |
| 1386 | if (WATCHDOG_ENABLE) { |
| 1387 | Watchdog.getInstance().addMonitor(this); |
| 1388 | } |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1391 | public void systemReady() { |
| 1392 | mSystemReady = true; |
| 1393 | mHandler.obtainMessage(H_SYSTEM_READY).sendToTarget(); |
| 1394 | } |
| 1395 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1396 | /** |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1397 | * Exposed API calls below here |
| 1398 | */ |
| 1399 | |
| 1400 | public void registerListener(IMountServiceListener listener) { |
| 1401 | synchronized (mListeners) { |
| 1402 | MountServiceBinderListener bl = new MountServiceBinderListener(listener); |
| 1403 | try { |
| 1404 | listener.asBinder().linkToDeath(bl, 0); |
| 1405 | mListeners.add(bl); |
| 1406 | } catch (RemoteException rex) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1407 | Slog.e(TAG, "Failed to link to listener death"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | public void unregisterListener(IMountServiceListener listener) { |
| 1413 | synchronized (mListeners) { |
| 1414 | for(MountServiceBinderListener bl : mListeners) { |
Niklas Brunlid | d64fe0f | 2013-07-05 08:54:03 +0200 | [diff] [blame] | 1415 | if (bl.mListener.asBinder() == listener.asBinder()) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1416 | mListeners.remove(mListeners.indexOf(bl)); |
Vairavan Srinivasan | 5c25a2d | 2012-01-24 08:22:14 -0800 | [diff] [blame] | 1417 | listener.asBinder().unlinkToDeath(bl, 0); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1418 | return; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 1424 | public void shutdown(final IMountShutdownObserver observer) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1425 | validatePermission(android.Manifest.permission.SHUTDOWN); |
| 1426 | |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1427 | Slog.i(TAG, "Shutting down"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1428 | synchronized (mVolumesLock) { |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 1429 | for (String path : mVolumeStates.keySet()) { |
| 1430 | String state = mVolumeStates.get(path); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1431 | |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 1432 | if (state.equals(Environment.MEDIA_SHARED)) { |
| 1433 | /* |
| 1434 | * If the media is currently shared, unshare it. |
| 1435 | * XXX: This is still dangerous!. We should not |
| 1436 | * be rebooting at *all* if UMS is enabled, since |
| 1437 | * the UMS host could have dirty FAT cache entries |
| 1438 | * yet to flush. |
| 1439 | */ |
| 1440 | setUsbMassStorageEnabled(false); |
| 1441 | } else if (state.equals(Environment.MEDIA_CHECKING)) { |
| 1442 | /* |
| 1443 | * If the media is being checked, then we need to wait for |
| 1444 | * it to complete before being able to proceed. |
| 1445 | */ |
| 1446 | // XXX: @hackbod - Should we disable the ANR timer here? |
| 1447 | int retries = 30; |
| 1448 | while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) { |
| 1449 | try { |
| 1450 | Thread.sleep(1000); |
| 1451 | } catch (InterruptedException iex) { |
| 1452 | Slog.e(TAG, "Interrupted while waiting for media", iex); |
| 1453 | break; |
| 1454 | } |
| 1455 | state = Environment.getExternalStorageState(); |
| 1456 | } |
| 1457 | if (retries == 0) { |
| 1458 | Slog.e(TAG, "Timed out waiting for media to check"); |
| 1459 | } |
San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 1460 | } |
San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 1461 | |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 1462 | if (state.equals(Environment.MEDIA_MOUNTED)) { |
| 1463 | // Post a unmount message. |
| 1464 | ShutdownCallBack ucb = new ShutdownCallBack(path, observer); |
| 1465 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb)); |
| 1466 | } else if (observer != null) { |
| 1467 | /* |
| 1468 | * Observer is waiting for onShutDownComplete when we are done. |
| 1469 | * Since nothing will be done send notification directly so shutdown |
| 1470 | * sequence can continue. |
| 1471 | */ |
| 1472 | try { |
| 1473 | observer.onShutDownComplete(StorageResultCode.OperationSucceeded); |
| 1474 | } catch (RemoteException e) { |
| 1475 | Slog.w(TAG, "RemoteException when shutting down"); |
| 1476 | } |
| 1477 | } |
Johan Alfven | 5d0db4d | 2010-11-09 10:32:25 +0100 | [diff] [blame] | 1478 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1482 | private boolean getUmsEnabling() { |
| 1483 | synchronized (mListeners) { |
| 1484 | return mUmsEnabling; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | private void setUmsEnabling(boolean enable) { |
| 1489 | synchronized (mListeners) { |
Tony Wu | fc71125 | 2010-08-09 16:49:19 +0800 | [diff] [blame] | 1490 | mUmsEnabling = enable; |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1494 | public boolean isUsbMassStorageConnected() { |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1495 | waitForReady(); |
San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 1496 | |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1497 | if (getUmsEnabling()) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1498 | return true; |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1499 | } |
Mike Lockwood | ecedfdc | 2011-06-08 15:11:59 -0700 | [diff] [blame] | 1500 | synchronized (mListeners) { |
| 1501 | return mUmsAvailable; |
| 1502 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1505 | public void setUsbMassStorageEnabled(boolean enable) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1506 | waitForReady(); |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1507 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1508 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1509 | final StorageVolume primary = getPrimaryPhysicalVolume(); |
| 1510 | if (primary == null) return; |
| 1511 | |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1512 | // TODO: Add support for multiple share methods |
| 1513 | |
| 1514 | /* |
| 1515 | * If the volume is mounted and we're enabling then unmount it |
| 1516 | */ |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1517 | String path = primary.getPath(); |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1518 | String vs = getVolumeState(path); |
| 1519 | String method = "ums"; |
| 1520 | if (enable && vs.equals(Environment.MEDIA_MOUNTED)) { |
| 1521 | // Override for isUsbMassStorageEnabled() |
| 1522 | setUmsEnabling(enable); |
| 1523 | UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true); |
| 1524 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb)); |
| 1525 | // Clear override |
| 1526 | setUmsEnabling(false); |
| 1527 | } |
| 1528 | /* |
| 1529 | * If we disabled UMS then mount the volume |
| 1530 | */ |
| 1531 | if (!enable) { |
| 1532 | doShareUnshareVolume(path, method, enable); |
| 1533 | if (doMountVolume(path) != StorageResultCode.OperationSucceeded) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1534 | Slog.e(TAG, "Failed to remount " + path + |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1535 | " after disabling share method " + method); |
| 1536 | /* |
| 1537 | * Even though the mount failed, the unshare didn't so don't indicate an error. |
| 1538 | * The mountVolume() call will have set the storage state and sent the necessary |
| 1539 | * broadcasts. |
| 1540 | */ |
| 1541 | } |
| 1542 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | public boolean isUsbMassStorageEnabled() { |
| 1546 | waitForReady(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1547 | |
| 1548 | final StorageVolume primary = getPrimaryPhysicalVolume(); |
| 1549 | if (primary != null) { |
| 1550 | return doGetVolumeShared(primary.getPath(), "ums"); |
| 1551 | } else { |
| 1552 | return false; |
| 1553 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1554 | } |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1555 | |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1556 | /** |
| 1557 | * @return state of the volume at the specified mount point |
| 1558 | */ |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1559 | public String getVolumeState(String mountPoint) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1560 | synchronized (mVolumesLock) { |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 1561 | String state = mVolumeStates.get(mountPoint); |
| 1562 | if (state == null) { |
| 1563 | Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume"); |
Ken Sumrall | 18db5c5 | 2011-07-14 11:35:06 -0700 | [diff] [blame] | 1564 | if (SystemProperties.get("vold.encrypt_progress").length() != 0) { |
| 1565 | state = Environment.MEDIA_REMOVED; |
| 1566 | } else { |
| 1567 | throw new IllegalArgumentException(); |
| 1568 | } |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 1569 | } |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1570 | |
Mike Lockwood | 7fa24aa | 2011-03-23 14:52:34 -0400 | [diff] [blame] | 1571 | return state; |
| 1572 | } |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1573 | } |
| 1574 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1575 | @Override |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1576 | public boolean isExternalStorageEmulated() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1577 | return mEmulatedTemplate != null; |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1580 | public int mountVolume(String path) { |
| 1581 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1582 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1583 | waitForReady(); |
| 1584 | return doMountVolume(path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 1587 | public void unmountVolume(String path, boolean force, boolean removeEncryption) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1588 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1589 | waitForReady(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1591 | String volState = getVolumeState(path); |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 1592 | if (DEBUG_UNMOUNT) { |
| 1593 | Slog.i(TAG, "Unmounting " + path |
| 1594 | + " force = " + force |
| 1595 | + " removeEncryption = " + removeEncryption); |
| 1596 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1597 | if (Environment.MEDIA_UNMOUNTED.equals(volState) || |
| 1598 | Environment.MEDIA_REMOVED.equals(volState) || |
| 1599 | Environment.MEDIA_SHARED.equals(volState) || |
| 1600 | Environment.MEDIA_UNMOUNTABLE.equals(volState)) { |
| 1601 | // Media already unmounted or cannot be unmounted. |
| 1602 | // TODO return valid return code when adding observer call back. |
| 1603 | return; |
| 1604 | } |
Ben Komalo | 13c7197 | 2011-09-07 16:35:56 -0700 | [diff] [blame] | 1605 | UnmountCallBack ucb = new UnmountCallBack(path, force, removeEncryption); |
Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 1606 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1609 | public int formatVolume(String path) { |
| 1610 | validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1611 | waitForReady(); |
San Mehat | 5b77dab | 2010-01-26 13:28:50 -0800 | [diff] [blame] | 1612 | |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1613 | return doFormatVolume(path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | } |
| 1615 | |
Mike Lockwood | ecedfdc | 2011-06-08 15:11:59 -0700 | [diff] [blame] | 1616 | public int[] getStorageUsers(String path) { |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1617 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1618 | waitForReady(); |
| 1619 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1620 | final String[] r = NativeDaemonEvent.filterMessageList( |
| 1621 | mConnector.executeForList("storage", "users", path), |
| 1622 | VoldResponseCode.StorageUsersListResult); |
| 1623 | |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1624 | // FMT: <pid> <process name> |
| 1625 | int[] data = new int[r.length]; |
| 1626 | for (int i = 0; i < r.length; i++) { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1627 | String[] tok = r[i].split(" "); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1628 | try { |
| 1629 | data[i] = Integer.parseInt(tok[0]); |
| 1630 | } catch (NumberFormatException nfe) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1631 | Slog.e(TAG, String.format("Error parsing pid %s", tok[0])); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1632 | return new int[0]; |
| 1633 | } |
| 1634 | } |
| 1635 | return data; |
| 1636 | } catch (NativeDaemonConnectorException e) { |
San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1637 | Slog.e(TAG, "Failed to retrieve storage users list", e); |
San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1638 | return new int[0]; |
| 1639 | } |
| 1640 | } |
| 1641 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1642 | private void warnOnNotMounted() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1643 | final StorageVolume primary = getPrimaryPhysicalVolume(); |
Jeff Sharkey | 32ee831 | 2012-09-30 13:21:31 -0700 | [diff] [blame] | 1644 | if (primary != null) { |
| 1645 | boolean mounted = false; |
| 1646 | try { |
| 1647 | mounted = Environment.MEDIA_MOUNTED.equals(getVolumeState(primary.getPath())); |
Jeff Sharkey | 9ae62f5 | 2013-03-26 10:29:01 -0700 | [diff] [blame] | 1648 | } catch (IllegalArgumentException e) { |
Jeff Sharkey | 32ee831 | 2012-09-30 13:21:31 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | if (!mounted) { |
| 1652 | Slog.w(TAG, "getSecureContainerList() called when storage not mounted"); |
| 1653 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1654 | } |
| 1655 | } |
| 1656 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1657 | public String[] getSecureContainerList() { |
| 1658 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1659 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1660 | warnOnNotMounted(); |
San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1661 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1662 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1663 | return NativeDaemonEvent.filterMessageList( |
| 1664 | mConnector.executeForList("asec", "list"), VoldResponseCode.AsecListResult); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1665 | } catch (NativeDaemonConnectorException e) { |
| 1666 | return new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | } |
| 1668 | } |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1669 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 1670 | public int createSecureContainer(String id, int sizeMb, String fstype, String key, |
| 1671 | int ownerUid, boolean external) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1672 | validatePermission(android.Manifest.permission.ASEC_CREATE); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1673 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1674 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1675 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1676 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1677 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 1678 | mConnector.execute("asec", "create", id, sizeMb, fstype, new SensitiveArg(key), |
| 1679 | ownerUid, external ? "1" : "0"); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1680 | } catch (NativeDaemonConnectorException e) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1681 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1682 | } |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1683 | |
| 1684 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1685 | synchronized (mAsecMountSet) { |
| 1686 | mAsecMountSet.add(id); |
| 1687 | } |
| 1688 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1689 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1690 | } |
| 1691 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1692 | public int finalizeSecureContainer(String id) { |
| 1693 | validatePermission(android.Manifest.permission.ASEC_CREATE); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1694 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1695 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1696 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1697 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1698 | mConnector.execute("asec", "finalize", id); |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1699 | /* |
| 1700 | * Finalization does a remount, so no need |
| 1701 | * to update mAsecMountSet |
| 1702 | */ |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1703 | } catch (NativeDaemonConnectorException e) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1704 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1705 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1706 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 1709 | public int fixPermissionsSecureContainer(String id, int gid, String filename) { |
| 1710 | validatePermission(android.Manifest.permission.ASEC_CREATE); |
| 1711 | warnOnNotMounted(); |
| 1712 | |
| 1713 | int rc = StorageResultCode.OperationSucceeded; |
| 1714 | try { |
| 1715 | mConnector.execute("asec", "fixperms", id, gid, filename); |
| 1716 | /* |
| 1717 | * Fix permissions does a remount, so no need to update |
| 1718 | * mAsecMountSet |
| 1719 | */ |
| 1720 | } catch (NativeDaemonConnectorException e) { |
| 1721 | rc = StorageResultCode.OperationFailedInternalError; |
| 1722 | } |
| 1723 | return rc; |
| 1724 | } |
| 1725 | |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1726 | public int destroySecureContainer(String id, boolean force) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1727 | validatePermission(android.Manifest.permission.ASEC_DESTROY); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1728 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1729 | warnOnNotMounted(); |
San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1730 | |
Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 1731 | /* |
| 1732 | * Force a GC to make sure AssetManagers in other threads of the |
| 1733 | * system_server are cleaned up. We have to do this since AssetManager |
| 1734 | * instances are kept as a WeakReference and it's possible we have files |
| 1735 | * open on the external storage. |
| 1736 | */ |
| 1737 | Runtime.getRuntime().gc(); |
| 1738 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1739 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1740 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1741 | final Command cmd = new Command("asec", "destroy", id); |
| 1742 | if (force) { |
| 1743 | cmd.appendArg("force"); |
| 1744 | } |
| 1745 | mConnector.execute(cmd); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1746 | } catch (NativeDaemonConnectorException e) { |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1747 | int code = e.getCode(); |
| 1748 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1749 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 1750 | } else { |
| 1751 | rc = StorageResultCode.OperationFailedInternalError; |
| 1752 | } |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1753 | } |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1754 | |
| 1755 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1756 | synchronized (mAsecMountSet) { |
| 1757 | if (mAsecMountSet.contains(id)) { |
| 1758 | mAsecMountSet.remove(id); |
| 1759 | } |
| 1760 | } |
| 1761 | } |
| 1762 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1763 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1764 | } |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1765 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1766 | public int mountSecureContainer(String id, String key, int ownerUid) { |
| 1767 | validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1768 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1769 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1770 | |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1771 | synchronized (mAsecMountSet) { |
| 1772 | if (mAsecMountSet.contains(id)) { |
| 1773 | return StorageResultCode.OperationFailedStorageMounted; |
| 1774 | } |
| 1775 | } |
| 1776 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1777 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1778 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 1779 | mConnector.execute("asec", "mount", id, new SensitiveArg(key), ownerUid); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1780 | } catch (NativeDaemonConnectorException e) { |
Kenny Root | f030462 | 2010-03-19 19:20:42 -0700 | [diff] [blame] | 1781 | int code = e.getCode(); |
| 1782 | if (code != VoldResponseCode.OpFailedStorageBusy) { |
| 1783 | rc = StorageResultCode.OperationFailedInternalError; |
| 1784 | } |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1785 | } |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1786 | |
| 1787 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1788 | synchronized (mAsecMountSet) { |
| 1789 | mAsecMountSet.add(id); |
| 1790 | } |
| 1791 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1792 | return rc; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1793 | } |
| 1794 | |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1795 | public int unmountSecureContainer(String id, boolean force) { |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1796 | validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1797 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1798 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1799 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1800 | synchronized (mAsecMountSet) { |
| 1801 | if (!mAsecMountSet.contains(id)) { |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1802 | return StorageResultCode.OperationFailedStorageNotMounted; |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | |
Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 1806 | /* |
| 1807 | * Force a GC to make sure AssetManagers in other threads of the |
| 1808 | * system_server are cleaned up. We have to do this since AssetManager |
| 1809 | * instances are kept as a WeakReference and it's possible we have files |
| 1810 | * open on the external storage. |
| 1811 | */ |
| 1812 | Runtime.getRuntime().gc(); |
| 1813 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1814 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1815 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1816 | final Command cmd = new Command("asec", "unmount", id); |
| 1817 | if (force) { |
| 1818 | cmd.appendArg("force"); |
| 1819 | } |
| 1820 | mConnector.execute(cmd); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1821 | } catch (NativeDaemonConnectorException e) { |
San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1822 | int code = e.getCode(); |
| 1823 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1824 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 1825 | } else { |
| 1826 | rc = StorageResultCode.OperationFailedInternalError; |
| 1827 | } |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1828 | } |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1829 | |
| 1830 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1831 | synchronized (mAsecMountSet) { |
| 1832 | mAsecMountSet.remove(id); |
| 1833 | } |
| 1834 | } |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1835 | return rc; |
San Mehat | 9dba709 | 2010-01-18 06:47:41 -0800 | [diff] [blame] | 1836 | } |
| 1837 | |
San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1838 | public boolean isSecureContainerMounted(String id) { |
| 1839 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
| 1840 | waitForReady(); |
| 1841 | warnOnNotMounted(); |
| 1842 | |
| 1843 | synchronized (mAsecMountSet) { |
| 1844 | return mAsecMountSet.contains(id); |
| 1845 | } |
| 1846 | } |
| 1847 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1848 | public int renameSecureContainer(String oldId, String newId) { |
| 1849 | validatePermission(android.Manifest.permission.ASEC_RENAME); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1850 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1851 | warnOnNotMounted(); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1852 | |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1853 | synchronized (mAsecMountSet) { |
San Mehat | 85451ee | 2010-02-24 08:54:18 -0800 | [diff] [blame] | 1854 | /* |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 1855 | * Because a mounted container has active internal state which cannot be |
San Mehat | 85451ee | 2010-02-24 08:54:18 -0800 | [diff] [blame] | 1856 | * changed while active, we must ensure both ids are not currently mounted. |
| 1857 | */ |
| 1858 | if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) { |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1859 | return StorageResultCode.OperationFailedStorageMounted; |
| 1860 | } |
| 1861 | } |
| 1862 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1863 | int rc = StorageResultCode.OperationSucceeded; |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1864 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1865 | mConnector.execute("asec", "rename", oldId, newId); |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1866 | } catch (NativeDaemonConnectorException e) { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1867 | rc = StorageResultCode.OperationFailedInternalError; |
San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1868 | } |
San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1869 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1870 | return rc; |
San Mehat | 45f6104 | 2010-01-23 08:12:43 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1873 | public String getSecureContainerPath(String id) { |
| 1874 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1875 | waitForReady(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1876 | warnOnNotMounted(); |
San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1877 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1878 | final NativeDaemonEvent event; |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1879 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1880 | event = mConnector.execute("asec", "path", id); |
| 1881 | event.checkCode(VoldResponseCode.AsecPathResult); |
| 1882 | return event.getMessage(); |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1883 | } catch (NativeDaemonConnectorException e) { |
| 1884 | int code = e.getCode(); |
| 1885 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
Fredrik Helmer | a20c8ef | 2011-02-09 16:16:10 +0100 | [diff] [blame] | 1886 | Slog.i(TAG, String.format("Container '%s' not found", id)); |
| 1887 | return null; |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1888 | } else { |
San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1889 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1890 | } |
| 1891 | } |
San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1892 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 1893 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 1894 | public String getSecureContainerFilesystemPath(String id) { |
| 1895 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
| 1896 | waitForReady(); |
| 1897 | warnOnNotMounted(); |
| 1898 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1899 | final NativeDaemonEvent event; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 1900 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1901 | event = mConnector.execute("asec", "fspath", id); |
| 1902 | event.checkCode(VoldResponseCode.AsecPathResult); |
| 1903 | return event.getMessage(); |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 1904 | } catch (NativeDaemonConnectorException e) { |
| 1905 | int code = e.getCode(); |
| 1906 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
| 1907 | Slog.i(TAG, String.format("Container '%s' not found", id)); |
| 1908 | return null; |
| 1909 | } else { |
| 1910 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 1915 | public void finishMediaUpdate() { |
| 1916 | mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE); |
| 1917 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1918 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1919 | private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) { |
| 1920 | if (callerUid == android.os.Process.SYSTEM_UID) { |
| 1921 | return true; |
| 1922 | } |
| 1923 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1924 | if (packageName == null) { |
| 1925 | return false; |
| 1926 | } |
| 1927 | |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1928 | final int packageUid = mPms.getPackageUid(packageName, UserHandle.getUserId(callerUid)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1929 | |
| 1930 | if (DEBUG_OBB) { |
| 1931 | Slog.d(TAG, "packageName = " + packageName + ", packageUid = " + |
| 1932 | packageUid + ", callerUid = " + callerUid); |
| 1933 | } |
| 1934 | |
| 1935 | return callerUid == packageUid; |
| 1936 | } |
| 1937 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1938 | public String getMountedObbPath(String rawPath) { |
| 1939 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 1940 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1941 | waitForReady(); |
| 1942 | warnOnNotMounted(); |
| 1943 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1944 | final ObbState state; |
| 1945 | synchronized (mObbPathToStateMap) { |
| 1946 | state = mObbPathToStateMap.get(rawPath); |
| 1947 | } |
| 1948 | if (state == null) { |
| 1949 | Slog.w(TAG, "Failed to find OBB mounted at " + rawPath); |
| 1950 | return null; |
| 1951 | } |
| 1952 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1953 | final NativeDaemonEvent event; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1954 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1955 | event = mConnector.execute("obb", "path", state.voldPath); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 1956 | event.checkCode(VoldResponseCode.AsecPathResult); |
| 1957 | return event.getMessage(); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1958 | } catch (NativeDaemonConnectorException e) { |
| 1959 | int code = e.getCode(); |
| 1960 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1961 | return null; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1962 | } else { |
| 1963 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1968 | @Override |
| 1969 | public boolean isObbMounted(String rawPath) { |
| 1970 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 1971 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1972 | return mObbPathToStateMap.containsKey(rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 1973 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1976 | @Override |
| 1977 | public void mountObb( |
| 1978 | String rawPath, String canonicalPath, String key, IObbActionListener token, int nonce) { |
| 1979 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
| 1980 | Preconditions.checkNotNull(canonicalPath, "canonicalPath cannot be null"); |
| 1981 | Preconditions.checkNotNull(token, "token cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 1982 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1983 | final int callingUid = Binder.getCallingUid(); |
| 1984 | final ObbState obbState = new ObbState(rawPath, canonicalPath, callingUid, token, nonce); |
| 1985 | final ObbAction action = new MountObbAction(obbState, key, callingUid); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1986 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
| 1987 | |
| 1988 | if (DEBUG_OBB) |
| 1989 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 1992 | @Override |
| 1993 | public void unmountObb(String rawPath, boolean force, IObbActionListener token, int nonce) { |
| 1994 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
| 1995 | |
| 1996 | final ObbState existingState; |
| 1997 | synchronized (mObbPathToStateMap) { |
| 1998 | existingState = mObbPathToStateMap.get(rawPath); |
Kenny Root | f1121dc | 2010-09-29 07:30:53 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2001 | if (existingState != null) { |
| 2002 | // TODO: separate state object from request data |
| 2003 | final int callingUid = Binder.getCallingUid(); |
| 2004 | final ObbState newState = new ObbState( |
| 2005 | rawPath, existingState.canonicalPath, callingUid, token, nonce); |
| 2006 | final ObbAction action = new UnmountObbAction(newState, force); |
| 2007 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2008 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2009 | if (DEBUG_OBB) |
| 2010 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
| 2011 | } else { |
| 2012 | Slog.w(TAG, "Unknown OBB mount at " + rawPath); |
| 2013 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2016 | @Override |
| 2017 | public int getEncryptionState() { |
| 2018 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2019 | "no permission to access the crypt keeper"); |
| 2020 | |
| 2021 | waitForReady(); |
| 2022 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2023 | final NativeDaemonEvent event; |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2024 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2025 | event = mConnector.execute("cryptfs", "cryptocomplete"); |
| 2026 | return Integer.parseInt(event.getMessage()); |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2027 | } catch (NumberFormatException e) { |
| 2028 | // Bad result - unexpected. |
| 2029 | Slog.w(TAG, "Unable to parse result from cryptfs cryptocomplete"); |
| 2030 | return ENCRYPTION_STATE_ERROR_UNKNOWN; |
| 2031 | } catch (NativeDaemonConnectorException e) { |
| 2032 | // Something bad happened. |
| 2033 | Slog.w(TAG, "Error in communicating with cryptfs in validating"); |
| 2034 | return ENCRYPTION_STATE_ERROR_UNKNOWN; |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | @Override |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2039 | public int decryptStorage(String password) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2040 | if (TextUtils.isEmpty(password)) { |
| 2041 | throw new IllegalArgumentException("password cannot be empty"); |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2042 | } |
| 2043 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2044 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2045 | "no permission to access the crypt keeper"); |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2046 | |
| 2047 | waitForReady(); |
| 2048 | |
| 2049 | if (DEBUG_EVENTS) { |
| 2050 | Slog.i(TAG, "decrypting storage..."); |
| 2051 | } |
| 2052 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2053 | final NativeDaemonEvent event; |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2054 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 2055 | event = mConnector.execute("cryptfs", "checkpw", new SensitiveArg(password)); |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2056 | |
Fredrik Roubert | da6aedf | 2011-12-20 17:34:43 +0100 | [diff] [blame] | 2057 | final int code = Integer.parseInt(event.getMessage()); |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2058 | if (code == 0) { |
| 2059 | // Decrypt was successful. Post a delayed message before restarting in order |
| 2060 | // to let the UI to clear itself |
| 2061 | mHandler.postDelayed(new Runnable() { |
| 2062 | public void run() { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 2063 | try { |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2064 | mConnector.execute("cryptfs", "restart"); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 2065 | } catch (NativeDaemonConnectorException e) { |
| 2066 | Slog.e(TAG, "problem executing in background", e); |
| 2067 | } |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2068 | } |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2069 | }, 1000); // 1 second |
Jason parks | 9ed98bc | 2011-01-17 09:58:35 -0600 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | return code; |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2073 | } catch (NativeDaemonConnectorException e) { |
| 2074 | // Decryption failed |
| 2075 | return e.getCode(); |
| 2076 | } |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2077 | } |
| 2078 | |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2079 | public int encryptStorage(String password) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2080 | if (TextUtils.isEmpty(password)) { |
| 2081 | throw new IllegalArgumentException("password cannot be empty"); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2082 | } |
| 2083 | |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2084 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2085 | "no permission to access the crypt keeper"); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2086 | |
| 2087 | waitForReady(); |
| 2088 | |
| 2089 | if (DEBUG_EVENTS) { |
Jason parks | 8888c59 | 2011-01-20 22:46:41 -0600 | [diff] [blame] | 2090 | Slog.i(TAG, "encrypting storage..."); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 2094 | mConnector.execute("cryptfs", "enablecrypto", "inplace", new SensitiveArg(password)); |
Jason parks | 56aa532 | 2011-01-07 09:01:15 -0600 | [diff] [blame] | 2095 | } catch (NativeDaemonConnectorException e) { |
| 2096 | // Encryption failed |
| 2097 | return e.getCode(); |
| 2098 | } |
| 2099 | |
| 2100 | return 0; |
| 2101 | } |
| 2102 | |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2103 | public int changeEncryptionPassword(String password) { |
| 2104 | if (TextUtils.isEmpty(password)) { |
| 2105 | throw new IllegalArgumentException("password cannot be empty"); |
| 2106 | } |
| 2107 | |
| 2108 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2109 | "no permission to access the crypt keeper"); |
| 2110 | |
| 2111 | waitForReady(); |
| 2112 | |
| 2113 | if (DEBUG_EVENTS) { |
| 2114 | Slog.i(TAG, "changing encryption password..."); |
| 2115 | } |
| 2116 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2117 | final NativeDaemonEvent event; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2118 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 2119 | event = mConnector.execute("cryptfs", "changepw", new SensitiveArg(password)); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2120 | return Integer.parseInt(event.getMessage()); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 2121 | } catch (NativeDaemonConnectorException e) { |
| 2122 | // Encryption failed |
| 2123 | return e.getCode(); |
| 2124 | } |
| 2125 | } |
| 2126 | |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2127 | /** |
| 2128 | * Validate a user-supplied password string with cryptfs |
| 2129 | */ |
| 2130 | @Override |
| 2131 | public int verifyEncryptionPassword(String password) throws RemoteException { |
| 2132 | // Only the system process is permitted to validate passwords |
| 2133 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 2134 | throw new SecurityException("no permission to access the crypt keeper"); |
| 2135 | } |
| 2136 | |
| 2137 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER, |
| 2138 | "no permission to access the crypt keeper"); |
| 2139 | |
| 2140 | if (TextUtils.isEmpty(password)) { |
| 2141 | throw new IllegalArgumentException("password cannot be empty"); |
| 2142 | } |
| 2143 | |
| 2144 | waitForReady(); |
| 2145 | |
| 2146 | if (DEBUG_EVENTS) { |
| 2147 | Slog.i(TAG, "validating encryption password..."); |
| 2148 | } |
| 2149 | |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2150 | final NativeDaemonEvent event; |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2151 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 2152 | event = mConnector.execute("cryptfs", "verifypw", new SensitiveArg(password)); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2153 | Slog.i(TAG, "cryptfs verifypw => " + event.getMessage()); |
| 2154 | return Integer.parseInt(event.getMessage()); |
Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 2155 | } catch (NativeDaemonConnectorException e) { |
| 2156 | // Encryption failed |
| 2157 | return e.getCode(); |
| 2158 | } |
| 2159 | } |
| 2160 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 2161 | @Override |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2162 | public int mkdirs(String callingPkg, String appPath) { |
| 2163 | final int userId = UserHandle.getUserId(Binder.getCallingUid()); |
| 2164 | final UserEnvironment userEnv = new UserEnvironment(userId); |
| 2165 | |
| 2166 | // Validate that reported package name belongs to caller |
| 2167 | final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService( |
| 2168 | Context.APP_OPS_SERVICE); |
| 2169 | appOps.checkPackage(Binder.getCallingUid(), callingPkg); |
| 2170 | |
| 2171 | try { |
| 2172 | appPath = new File(appPath).getCanonicalPath(); |
| 2173 | } catch (IOException e) { |
| 2174 | Slog.e(TAG, "Failed to resolve " + appPath + ": " + e); |
| 2175 | return -1; |
| 2176 | } |
| 2177 | |
Jeff Sharkey | 5786a27 | 2013-10-02 12:50:34 -0700 | [diff] [blame] | 2178 | if (!appPath.endsWith("/")) { |
| 2179 | appPath = appPath + "/"; |
| 2180 | } |
| 2181 | |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2182 | // Try translating the app path into a vold path, but require that it |
| 2183 | // belong to the calling package. |
| 2184 | String voldPath = maybeTranslatePathForVold(appPath, |
| 2185 | userEnv.buildExternalStorageAppDataDirs(callingPkg), |
| 2186 | userEnv.buildExternalStorageAppDataDirsForVold(callingPkg)); |
| 2187 | if (voldPath != null) { |
| 2188 | try { |
| 2189 | mConnector.execute("volume", "mkdirs", voldPath); |
| 2190 | return 0; |
| 2191 | } catch (NativeDaemonConnectorException e) { |
| 2192 | return e.getCode(); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | voldPath = maybeTranslatePathForVold(appPath, |
| 2197 | userEnv.buildExternalStorageAppObbDirs(callingPkg), |
| 2198 | userEnv.buildExternalStorageAppObbDirsForVold(callingPkg)); |
| 2199 | if (voldPath != null) { |
| 2200 | try { |
| 2201 | mConnector.execute("volume", "mkdirs", voldPath); |
| 2202 | return 0; |
| 2203 | } catch (NativeDaemonConnectorException e) { |
| 2204 | return e.getCode(); |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | throw new SecurityException("Invalid mkdirs path: " + appPath); |
| 2209 | } |
| 2210 | |
| 2211 | /** |
| 2212 | * Translate the given path from an app-visible path to a vold-visible path, |
| 2213 | * but only if it's under the given whitelisted paths. |
| 2214 | * |
| 2215 | * @param path a canonicalized app-visible path. |
| 2216 | * @param appPaths list of app-visible paths that are allowed. |
| 2217 | * @param voldPaths list of vold-visible paths directly corresponding to the |
| 2218 | * allowed app-visible paths argument. |
| 2219 | * @return a vold-visible path representing the original path, or |
| 2220 | * {@code null} if the given path didn't have an app-to-vold |
| 2221 | * mapping. |
| 2222 | */ |
| 2223 | @VisibleForTesting |
| 2224 | public static String maybeTranslatePathForVold( |
| 2225 | String path, File[] appPaths, File[] voldPaths) { |
| 2226 | if (appPaths.length != voldPaths.length) { |
| 2227 | throw new IllegalStateException("Paths must be 1:1 mapping"); |
| 2228 | } |
| 2229 | |
| 2230 | for (int i = 0; i < appPaths.length; i++) { |
Jeff Sharkey | 5786a27 | 2013-10-02 12:50:34 -0700 | [diff] [blame] | 2231 | final String appPath = appPaths[i].getAbsolutePath() + "/"; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2232 | if (path.startsWith(appPath)) { |
Jeff Sharkey | 5786a27 | 2013-10-02 12:50:34 -0700 | [diff] [blame] | 2233 | path = new File(voldPaths[i], path.substring(appPath.length())) |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2234 | .getAbsolutePath(); |
| 2235 | if (!path.endsWith("/")) { |
| 2236 | path = path + "/"; |
| 2237 | } |
| 2238 | return path; |
| 2239 | } |
| 2240 | } |
| 2241 | return null; |
| 2242 | } |
| 2243 | |
| 2244 | @Override |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 2245 | public StorageVolume[] getVolumeList() { |
| 2246 | final int callingUserId = UserHandle.getCallingUserId(); |
| 2247 | final boolean accessAll = (mContext.checkPermission( |
| 2248 | android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE, |
| 2249 | Binder.getCallingPid(), Binder.getCallingUid()) == PERMISSION_GRANTED); |
| 2250 | |
| 2251 | synchronized (mVolumesLock) { |
| 2252 | final ArrayList<StorageVolume> filtered = Lists.newArrayList(); |
| 2253 | for (StorageVolume volume : mVolumes) { |
| 2254 | final UserHandle owner = volume.getOwner(); |
| 2255 | final boolean ownerMatch = owner == null || owner.getIdentifier() == callingUserId; |
| 2256 | if (accessAll || ownerMatch) { |
| 2257 | filtered.add(volume); |
| 2258 | } |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 2259 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 2260 | return filtered.toArray(new StorageVolume[filtered.size()]); |
Mike Lockwood | 8fa5f80 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 2261 | } |
| 2262 | } |
| 2263 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2264 | private void addObbStateLocked(ObbState obbState) throws RemoteException { |
| 2265 | final IBinder binder = obbState.getBinder(); |
| 2266 | List<ObbState> obbStates = mObbMounts.get(binder); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 2267 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2268 | if (obbStates == null) { |
| 2269 | obbStates = new ArrayList<ObbState>(); |
| 2270 | mObbMounts.put(binder, obbStates); |
| 2271 | } else { |
| 2272 | for (final ObbState o : obbStates) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2273 | if (o.rawPath.equals(obbState.rawPath)) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2274 | throw new IllegalStateException("Attempt to add ObbState twice. " |
| 2275 | + "This indicates an error in the MountService logic."); |
Kenny Root | 5919ac6 | 2010-10-05 09:49:40 -0700 | [diff] [blame] | 2276 | } |
| 2277 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2278 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2279 | |
| 2280 | obbStates.add(obbState); |
| 2281 | try { |
| 2282 | obbState.link(); |
| 2283 | } catch (RemoteException e) { |
| 2284 | /* |
| 2285 | * The binder died before we could link it, so clean up our state |
| 2286 | * and return failure. |
| 2287 | */ |
| 2288 | obbStates.remove(obbState); |
| 2289 | if (obbStates.isEmpty()) { |
| 2290 | mObbMounts.remove(binder); |
| 2291 | } |
| 2292 | |
| 2293 | // Rethrow the error so mountObb can get it |
| 2294 | throw e; |
| 2295 | } |
| 2296 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2297 | mObbPathToStateMap.put(obbState.rawPath, obbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2298 | } |
| 2299 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2300 | private void removeObbStateLocked(ObbState obbState) { |
| 2301 | final IBinder binder = obbState.getBinder(); |
| 2302 | final List<ObbState> obbStates = mObbMounts.get(binder); |
| 2303 | if (obbStates != null) { |
| 2304 | if (obbStates.remove(obbState)) { |
| 2305 | obbState.unlink(); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 2306 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2307 | if (obbStates.isEmpty()) { |
| 2308 | mObbMounts.remove(binder); |
| 2309 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2310 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2311 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2312 | mObbPathToStateMap.remove(obbState.rawPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2315 | private class ObbActionHandler extends Handler { |
| 2316 | private boolean mBound = false; |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 2317 | private final List<ObbAction> mActions = new LinkedList<ObbAction>(); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2318 | |
| 2319 | ObbActionHandler(Looper l) { |
| 2320 | super(l); |
| 2321 | } |
| 2322 | |
| 2323 | @Override |
| 2324 | public void handleMessage(Message msg) { |
| 2325 | switch (msg.what) { |
| 2326 | case OBB_RUN_ACTION: { |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 2327 | final ObbAction action = (ObbAction) msg.obj; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2328 | |
| 2329 | if (DEBUG_OBB) |
| 2330 | Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString()); |
| 2331 | |
| 2332 | // If a bind was already initiated we don't really |
| 2333 | // need to do anything. The pending install |
| 2334 | // will be processed later on. |
| 2335 | if (!mBound) { |
| 2336 | // If this is the only one pending we might |
| 2337 | // have to bind to the service again. |
| 2338 | if (!connectToService()) { |
| 2339 | Slog.e(TAG, "Failed to bind to media container service"); |
| 2340 | action.handleError(); |
| 2341 | return; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2342 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2343 | } |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 2344 | |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 2345 | mActions.add(action); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2346 | break; |
| 2347 | } |
| 2348 | case OBB_MCS_BOUND: { |
| 2349 | if (DEBUG_OBB) |
| 2350 | Slog.i(TAG, "OBB_MCS_BOUND"); |
| 2351 | if (msg.obj != null) { |
| 2352 | mContainerService = (IMediaContainerService) msg.obj; |
| 2353 | } |
| 2354 | if (mContainerService == null) { |
| 2355 | // Something seriously wrong. Bail out |
| 2356 | Slog.e(TAG, "Cannot bind to media container service"); |
| 2357 | for (ObbAction action : mActions) { |
| 2358 | // Indicate service bind error |
| 2359 | action.handleError(); |
| 2360 | } |
| 2361 | mActions.clear(); |
| 2362 | } else if (mActions.size() > 0) { |
Kenny Root | 480afe7 | 2010-10-07 10:17:50 -0700 | [diff] [blame] | 2363 | final ObbAction action = mActions.get(0); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2364 | if (action != null) { |
| 2365 | action.execute(this); |
| 2366 | } |
| 2367 | } else { |
| 2368 | // Should never happen ideally. |
| 2369 | Slog.w(TAG, "Empty queue"); |
| 2370 | } |
| 2371 | break; |
| 2372 | } |
| 2373 | case OBB_MCS_RECONNECT: { |
| 2374 | if (DEBUG_OBB) |
| 2375 | Slog.i(TAG, "OBB_MCS_RECONNECT"); |
| 2376 | if (mActions.size() > 0) { |
| 2377 | if (mBound) { |
| 2378 | disconnectService(); |
| 2379 | } |
| 2380 | if (!connectToService()) { |
| 2381 | Slog.e(TAG, "Failed to bind to media container service"); |
| 2382 | for (ObbAction action : mActions) { |
| 2383 | // Indicate service bind error |
| 2384 | action.handleError(); |
| 2385 | } |
| 2386 | mActions.clear(); |
| 2387 | } |
| 2388 | } |
| 2389 | break; |
| 2390 | } |
| 2391 | case OBB_MCS_UNBIND: { |
| 2392 | if (DEBUG_OBB) |
| 2393 | Slog.i(TAG, "OBB_MCS_UNBIND"); |
| 2394 | |
| 2395 | // Delete pending install |
| 2396 | if (mActions.size() > 0) { |
| 2397 | mActions.remove(0); |
| 2398 | } |
| 2399 | if (mActions.size() == 0) { |
| 2400 | if (mBound) { |
| 2401 | disconnectService(); |
| 2402 | } |
| 2403 | } else { |
| 2404 | // There are more pending requests in queue. |
| 2405 | // Just post MCS_BOUND message to trigger processing |
| 2406 | // of next pending install. |
| 2407 | mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND); |
| 2408 | } |
| 2409 | break; |
| 2410 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2411 | case OBB_FLUSH_MOUNT_STATE: { |
| 2412 | final String path = (String) msg.obj; |
| 2413 | |
| 2414 | if (DEBUG_OBB) |
| 2415 | Slog.i(TAG, "Flushing all OBB state for path " + path); |
| 2416 | |
| 2417 | synchronized (mObbMounts) { |
| 2418 | final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>(); |
| 2419 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2420 | final Iterator<ObbState> i = mObbPathToStateMap.values().iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2421 | while (i.hasNext()) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2422 | final ObbState state = i.next(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2423 | |
| 2424 | /* |
| 2425 | * If this entry's source file is in the volume path |
| 2426 | * that got unmounted, remove it because it's no |
| 2427 | * longer valid. |
| 2428 | */ |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2429 | if (state.canonicalPath.startsWith(path)) { |
| 2430 | obbStatesToRemove.add(state); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | for (final ObbState obbState : obbStatesToRemove) { |
| 2435 | if (DEBUG_OBB) |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2436 | Slog.i(TAG, "Removing state for " + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2437 | |
| 2438 | removeObbStateLocked(obbState); |
| 2439 | |
| 2440 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2441 | obbState.token.onObbResult(obbState.rawPath, obbState.nonce, |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2442 | OnObbStateChangeListener.UNMOUNTED); |
| 2443 | } catch (RemoteException e) { |
| 2444 | Slog.i(TAG, "Couldn't send unmount notification for OBB: " |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2445 | + obbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2446 | } |
| 2447 | } |
| 2448 | } |
| 2449 | break; |
| 2450 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | private boolean connectToService() { |
| 2455 | if (DEBUG_OBB) |
| 2456 | Slog.i(TAG, "Trying to bind to DefaultContainerService"); |
| 2457 | |
| 2458 | Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 2459 | if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) { |
| 2460 | mBound = true; |
| 2461 | return true; |
| 2462 | } |
| 2463 | return false; |
| 2464 | } |
| 2465 | |
| 2466 | private void disconnectService() { |
| 2467 | mContainerService = null; |
| 2468 | mBound = false; |
| 2469 | mContext.unbindService(mDefContainerConn); |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | abstract class ObbAction { |
| 2474 | private static final int MAX_RETRIES = 3; |
| 2475 | private int mRetries; |
| 2476 | |
| 2477 | ObbState mObbState; |
| 2478 | |
| 2479 | ObbAction(ObbState obbState) { |
| 2480 | mObbState = obbState; |
| 2481 | } |
| 2482 | |
| 2483 | public void execute(ObbActionHandler handler) { |
| 2484 | try { |
| 2485 | if (DEBUG_OBB) |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2486 | Slog.i(TAG, "Starting to execute action: " + toString()); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2487 | mRetries++; |
| 2488 | if (mRetries > MAX_RETRIES) { |
| 2489 | 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] | 2490 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2491 | handleError(); |
| 2492 | return; |
| 2493 | } else { |
| 2494 | handleExecute(); |
| 2495 | if (DEBUG_OBB) |
| 2496 | Slog.i(TAG, "Posting install MCS_UNBIND"); |
| 2497 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
| 2498 | } |
| 2499 | } catch (RemoteException e) { |
| 2500 | if (DEBUG_OBB) |
| 2501 | Slog.i(TAG, "Posting install MCS_RECONNECT"); |
| 2502 | mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT); |
| 2503 | } catch (Exception e) { |
| 2504 | if (DEBUG_OBB) |
| 2505 | Slog.d(TAG, "Error handling OBB action", e); |
| 2506 | handleError(); |
Kenny Root | 17eb6fb | 2010-10-06 15:02:52 -0700 | [diff] [blame] | 2507 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2508 | } |
| 2509 | } |
| 2510 | |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 2511 | abstract void handleExecute() throws RemoteException, IOException; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2512 | abstract void handleError(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2513 | |
| 2514 | protected ObbInfo getObbInfo() throws IOException { |
| 2515 | ObbInfo obbInfo; |
| 2516 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2517 | obbInfo = mContainerService.getObbInfo(mObbState.ownerPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2518 | } catch (RemoteException e) { |
| 2519 | Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for " |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2520 | + mObbState.ownerPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2521 | obbInfo = null; |
| 2522 | } |
| 2523 | if (obbInfo == null) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2524 | throw new IOException("Couldn't read OBB file: " + mObbState.ownerPath); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2525 | } |
| 2526 | return obbInfo; |
| 2527 | } |
| 2528 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2529 | protected void sendNewStatusOrIgnore(int status) { |
| 2530 | if (mObbState == null || mObbState.token == null) { |
| 2531 | return; |
| 2532 | } |
| 2533 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2534 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2535 | mObbState.token.onObbResult(mObbState.rawPath, mObbState.nonce, status); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2536 | } catch (RemoteException e) { |
| 2537 | Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged"); |
| 2538 | } |
| 2539 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | class MountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2543 | private final String mKey; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2544 | private final int mCallingUid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2545 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2546 | MountObbAction(ObbState obbState, String key, int callingUid) { |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2547 | super(obbState); |
| 2548 | mKey = key; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2549 | mCallingUid = callingUid; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2550 | } |
| 2551 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2552 | @Override |
Kenny Root | 735de3b | 2010-09-30 14:11:39 -0700 | [diff] [blame] | 2553 | public void handleExecute() throws IOException, RemoteException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2554 | waitForReady(); |
| 2555 | warnOnNotMounted(); |
| 2556 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2557 | final ObbInfo obbInfo = getObbInfo(); |
| 2558 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2559 | if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mCallingUid)) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2560 | Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename |
| 2561 | + " which is owned by " + obbInfo.packageName); |
| 2562 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED); |
| 2563 | return; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2564 | } |
| 2565 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2566 | final boolean isMounted; |
| 2567 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2568 | isMounted = mObbPathToStateMap.containsKey(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2569 | } |
| 2570 | if (isMounted) { |
| 2571 | Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename); |
| 2572 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED); |
| 2573 | return; |
| 2574 | } |
| 2575 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2576 | final String hashedKey; |
| 2577 | if (mKey == null) { |
| 2578 | hashedKey = "none"; |
| 2579 | } else { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2580 | try { |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 2581 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 2582 | |
| 2583 | KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt, |
| 2584 | PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE); |
| 2585 | SecretKey key = factory.generateSecret(ks); |
| 2586 | BigInteger bi = new BigInteger(key.getEncoded()); |
| 2587 | hashedKey = bi.toString(16); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2588 | } catch (NoSuchAlgorithmException e) { |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 2589 | Slog.e(TAG, "Could not load PBKDF2 algorithm", e); |
| 2590 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
| 2591 | return; |
| 2592 | } catch (InvalidKeySpecException e) { |
| 2593 | Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e); |
| 2594 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2595 | return; |
| 2596 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2597 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2598 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2599 | int rc = StorageResultCode.OperationSucceeded; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2600 | try { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 2601 | mConnector.execute("obb", "mount", mObbState.voldPath, new SensitiveArg(hashedKey), |
| 2602 | mObbState.ownerGid); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2603 | } catch (NativeDaemonConnectorException e) { |
| 2604 | int code = e.getCode(); |
| 2605 | if (code != VoldResponseCode.OpFailedStorageBusy) { |
| 2606 | rc = StorageResultCode.OperationFailedInternalError; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2607 | } |
| 2608 | } |
| 2609 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2610 | if (rc == StorageResultCode.OperationSucceeded) { |
| 2611 | if (DEBUG_OBB) |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2612 | Slog.d(TAG, "Successfully mounted OBB " + mObbState.voldPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2613 | |
| 2614 | synchronized (mObbMounts) { |
| 2615 | addObbStateLocked(mObbState); |
| 2616 | } |
| 2617 | |
| 2618 | sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2619 | } else { |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 2620 | Slog.e(TAG, "Couldn't mount OBB file: " + rc); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2621 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2622 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2623 | } |
| 2624 | } |
| 2625 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2626 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2627 | public void handleError() { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2628 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2629 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2630 | |
| 2631 | @Override |
| 2632 | public String toString() { |
| 2633 | StringBuilder sb = new StringBuilder(); |
| 2634 | sb.append("MountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2635 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2636 | sb.append('}'); |
| 2637 | return sb.toString(); |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | class UnmountObbAction extends ObbAction { |
Ben Komalo | 444eca2 | 2011-09-01 15:17:44 -0700 | [diff] [blame] | 2642 | private final boolean mForceUnmount; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2643 | |
| 2644 | UnmountObbAction(ObbState obbState, boolean force) { |
| 2645 | super(obbState); |
| 2646 | mForceUnmount = force; |
| 2647 | } |
| 2648 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2649 | @Override |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2650 | public void handleExecute() throws IOException { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2651 | waitForReady(); |
| 2652 | warnOnNotMounted(); |
| 2653 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2654 | final ObbInfo obbInfo = getObbInfo(); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2655 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2656 | final ObbState existingState; |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2657 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2658 | existingState = mObbPathToStateMap.get(mObbState.rawPath); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2659 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2660 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2661 | if (existingState == null) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2662 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED); |
| 2663 | return; |
| 2664 | } |
| 2665 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2666 | if (existingState.ownerGid != mObbState.ownerGid) { |
| 2667 | Slog.w(TAG, "Permission denied attempting to unmount OBB " + existingState.rawPath |
| 2668 | + " (owned by GID " + existingState.ownerGid + ")"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2669 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED); |
| 2670 | return; |
| 2671 | } |
| 2672 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2673 | int rc = StorageResultCode.OperationSucceeded; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2674 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2675 | final Command cmd = new Command("obb", "unmount", mObbState.voldPath); |
Jeff Sharkey | dd519fa | 2011-12-02 14:11:21 -0800 | [diff] [blame] | 2676 | if (mForceUnmount) { |
| 2677 | cmd.appendArg("force"); |
| 2678 | } |
| 2679 | mConnector.execute(cmd); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2680 | } catch (NativeDaemonConnectorException e) { |
| 2681 | int code = e.getCode(); |
| 2682 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 2683 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 2684 | } else if (code == VoldResponseCode.OpFailedStorageNotFound) { |
| 2685 | // If it's not mounted then we've already won. |
| 2686 | rc = StorageResultCode.OperationSucceeded; |
| 2687 | } else { |
| 2688 | rc = StorageResultCode.OperationFailedInternalError; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2689 | } |
| 2690 | } |
| 2691 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2692 | if (rc == StorageResultCode.OperationSucceeded) { |
| 2693 | synchronized (mObbMounts) { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2694 | removeObbStateLocked(existingState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2695 | } |
| 2696 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2697 | sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2698 | } else { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2699 | Slog.w(TAG, "Could not unmount OBB: " + existingState); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2700 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2701 | } |
| 2702 | } |
| 2703 | |
Jason parks | 5af0b91 | 2010-11-29 09:05:25 -0600 | [diff] [blame] | 2704 | @Override |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2705 | public void handleError() { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2706 | sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | @Override |
| 2710 | public String toString() { |
| 2711 | StringBuilder sb = new StringBuilder(); |
| 2712 | sb.append("UnmountObbAction{"); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2713 | sb.append(mObbState); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2714 | sb.append(",force="); |
| 2715 | sb.append(mForceUnmount); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 2716 | sb.append('}'); |
| 2717 | return sb.toString(); |
| 2718 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 2719 | } |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2720 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 2721 | @VisibleForTesting |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2722 | public static String buildObbPath(final String canonicalPath, int userId, boolean forVold) { |
| 2723 | // TODO: allow caller to provide Environment for full testing |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2724 | // TODO: extend to support OBB mounts on secondary external storage |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2725 | |
| 2726 | // Only adjust paths when storage is emulated |
| 2727 | if (!Environment.isExternalStorageEmulated()) { |
| 2728 | return canonicalPath; |
| 2729 | } |
| 2730 | |
| 2731 | String path = canonicalPath.toString(); |
| 2732 | |
| 2733 | // First trim off any external storage prefix |
| 2734 | final UserEnvironment userEnv = new UserEnvironment(userId); |
| 2735 | |
| 2736 | // /storage/emulated/0 |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2737 | final String externalPath = userEnv.getExternalStorageDirectory().getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2738 | // /storage/emulated_legacy |
| 2739 | final String legacyExternalPath = Environment.getLegacyExternalStorageDirectory() |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2740 | .getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2741 | |
| 2742 | if (path.startsWith(externalPath)) { |
| 2743 | path = path.substring(externalPath.length() + 1); |
| 2744 | } else if (path.startsWith(legacyExternalPath)) { |
| 2745 | path = path.substring(legacyExternalPath.length() + 1); |
| 2746 | } else { |
| 2747 | return canonicalPath; |
| 2748 | } |
| 2749 | |
| 2750 | // Handle special OBB paths on emulated storage |
| 2751 | final String obbPath = "Android/obb"; |
| 2752 | if (path.startsWith(obbPath)) { |
| 2753 | path = path.substring(obbPath.length() + 1); |
| 2754 | |
| 2755 | if (forVold) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2756 | return new File(Environment.getEmulatedStorageObbSource(), path).getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2757 | } else { |
| 2758 | final UserEnvironment ownerEnv = new UserEnvironment(UserHandle.USER_OWNER); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2759 | return new File(ownerEnv.buildExternalStorageAndroidObbDirs()[0], path) |
| 2760 | .getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | // Handle normal external storage paths |
| 2765 | if (forVold) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2766 | return new File(Environment.getEmulatedStorageSource(userId), path).getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2767 | } else { |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2768 | return new File(userEnv.getExternalDirsForApp()[0], path).getAbsolutePath(); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2772 | @Override |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2773 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 2774 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 2775 | |
| 2776 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ", 160); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2777 | |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2778 | synchronized (mObbMounts) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2779 | pw.println("mObbMounts:"); |
| 2780 | pw.increaseIndent(); |
| 2781 | final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet() |
| 2782 | .iterator(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2783 | while (binders.hasNext()) { |
| 2784 | Entry<IBinder, List<ObbState>> e = binders.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2785 | pw.println(e.getKey() + ":"); |
| 2786 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2787 | final List<ObbState> obbStates = e.getValue(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2788 | for (final ObbState obbState : obbStates) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2789 | pw.println(obbState); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2790 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2791 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2792 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2793 | pw.decreaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2794 | |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2795 | pw.println(); |
| 2796 | pw.println("mObbPathToStateMap:"); |
| 2797 | pw.increaseIndent(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2798 | final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator(); |
| 2799 | while (maps.hasNext()) { |
| 2800 | final Entry<String, ObbState> e = maps.next(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2801 | pw.print(e.getKey()); |
| 2802 | pw.print(" -> "); |
| 2803 | pw.println(e.getValue()); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 2804 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2805 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2806 | } |
Kenny Root | 4161f9b | 2011-07-13 09:48:33 -0700 | [diff] [blame] | 2807 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 2808 | synchronized (mVolumesLock) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2809 | pw.println(); |
| 2810 | pw.println("mVolumes:"); |
| 2811 | pw.increaseIndent(); |
| 2812 | for (StorageVolume volume : mVolumes) { |
| 2813 | pw.println(volume); |
| 2814 | pw.increaseIndent(); |
| 2815 | pw.println("Current state: " + mVolumeStates.get(volume.getPath())); |
| 2816 | pw.decreaseIndent(); |
Kenny Root | 4161f9b | 2011-07-13 09:48:33 -0700 | [diff] [blame] | 2817 | } |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2818 | pw.decreaseIndent(); |
Kenny Root | 4161f9b | 2011-07-13 09:48:33 -0700 | [diff] [blame] | 2819 | } |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 2820 | |
| 2821 | pw.println(); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2822 | pw.println("mConnection:"); |
| 2823 | pw.increaseIndent(); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 2824 | mConnector.dump(fd, pw, args); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 2825 | pw.decreaseIndent(); |
Kenny Root | 38cf886 | 2010-09-26 14:18:51 -0700 | [diff] [blame] | 2826 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2827 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 2828 | /** {@inheritDoc} */ |
| 2829 | public void monitor() { |
| 2830 | if (mConnector != null) { |
| 2831 | mConnector.monitor(); |
| 2832 | } |
| 2833 | } |
| 2834 | } |