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