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