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