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