| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | ** Copyright 2008, The Android Open Source Project | 
|  | 3 | ** | 
| Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 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 | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** | 
| Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 8 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** | 
| Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 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 | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 17 | #include "InstalldNativeService.h" | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 18 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER | 
|  | 20 |  | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 21 | #include <errno.h> | 
|  | 22 | #include <inttypes.h> | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 23 | #include <fstream> | 
|  | 24 | #include <fts.h> | 
| Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 25 | #include <regex> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 26 | #include <stdlib.h> | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 27 | #include <string.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 28 | #include <sys/capability.h> | 
|  | 29 | #include <sys/file.h> | 
|  | 30 | #include <sys/resource.h> | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 31 | #include <sys/quota.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 33 | #include <sys/statvfs.h> | 
| Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 34 | #include <sys/types.h> | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 35 | #include <sys/wait.h> | 
| Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 36 | #include <sys/xattr.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 37 | #include <unistd.h> | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 38 |  | 
| Andreas Gampe | afa58d1 | 2016-06-03 16:09:32 -0700 | [diff] [blame] | 39 | #include <android-base/logging.h> | 
| Elliott Hughes | e4ec9eb | 2015-12-04 15:39:32 -0800 | [diff] [blame] | 40 | #include <android-base/stringprintf.h> | 
| David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 41 | #include <android-base/strings.h> | 
| Roland Levillain | 64b59cc | 2016-04-05 16:41:12 +0100 | [diff] [blame] | 42 | #include <android-base/unique_fd.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 43 | #include <cutils/fs.h> | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 44 | #include <cutils/properties.h> | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 45 | #include <cutils/sched_policy.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 46 | #include <log/log.h>               // TODO: Move everything to base/logging. | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 47 | #include <logwrap/logwrap.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 48 | #include <private/android_filesystem_config.h> | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 49 | #include <selinux/android.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 50 | #include <system/thread_defs.h> | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 51 | #include <utils/Trace.h> | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 52 |  | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 53 | #include "dexopt.h" | 
| Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 54 | #include "globals.h" | 
|  | 55 | #include "installd_deps.h" | 
|  | 56 | #include "otapreopt_utils.h" | 
|  | 57 | #include "utils.h" | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 58 |  | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 59 | #include "CacheTracker.h" | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 60 | #include "MatchExtensionGen.h" | 
|  | 61 |  | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 62 | #ifndef LOG_TAG | 
|  | 63 | #define LOG_TAG "installd" | 
|  | 64 | #endif | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 65 |  | 
|  | 66 | using android::base::StringPrintf; | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 67 | using std::endl; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 68 |  | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 69 | namespace android { | 
|  | 70 | namespace installd { | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 71 |  | 
| Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 72 | static constexpr const char* kCpPath = "/system/bin/cp"; | 
|  | 73 | static constexpr const char* kXattrDefault = "user.default"; | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 74 |  | 
| Janis Danisevskis | 40bd3ef | 2016-06-07 10:31:27 -0700 | [diff] [blame] | 75 | static constexpr const int MIN_RESTRICTED_HOME_SDK_VERSION = 24; // > M | 
| Janis Danisevskis | eecb2d2 | 2016-01-12 14:45:55 +0000 | [diff] [blame] | 76 |  | 
| Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 77 | static constexpr const char* PKG_LIB_POSTFIX = "/lib"; | 
|  | 78 | static constexpr const char* CACHE_DIR_POSTFIX = "/cache"; | 
|  | 79 | static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; | 
|  | 80 |  | 
|  | 81 | static constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; | 
|  | 82 | static constexpr const char* IDMAP_SUFFIX = "@idmap"; | 
|  | 83 |  | 
| Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 84 | // NOTE: keep in sync with Installer | 
|  | 85 | static constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; | 
|  | 86 | static constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 87 | static constexpr int FLAG_USE_QUOTA = 1 << 12; | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 88 | static constexpr int FLAG_FREE_CACHE_V2 = 1 << 13; | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 89 | static constexpr int FLAG_FREE_CACHE_V2_DEFY_QUOTA = 1 << 14; | 
|  | 90 | static constexpr int FLAG_FREE_CACHE_NOOP = 1 << 15; | 
| Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 91 | static constexpr int FLAG_FORCE = 1 << 16; | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 92 |  | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 93 | namespace { | 
|  | 94 |  | 
|  | 95 | constexpr const char* kDump = "android.permission.DUMP"; | 
|  | 96 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 97 | static binder::Status ok() { | 
|  | 98 | return binder::Status::ok(); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | static binder::Status exception(uint32_t code, const std::string& msg) { | 
|  | 102 | return binder::Status::fromExceptionCode(code, String8(msg.c_str())); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | static binder::Status error() { | 
|  | 106 | return binder::Status::fromServiceSpecificError(errno); | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | static binder::Status error(const std::string& msg) { | 
|  | 110 | PLOG(ERROR) << msg; | 
|  | 111 | return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | static binder::Status error(uint32_t code, const std::string& msg) { | 
|  | 115 | LOG(ERROR) << msg << " (" << code << ")"; | 
|  | 116 | return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); | 
|  | 117 | } | 
|  | 118 |  | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 119 | binder::Status checkPermission(const char* permission) { | 
|  | 120 | pid_t pid; | 
|  | 121 | uid_t uid; | 
|  | 122 |  | 
|  | 123 | if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), | 
|  | 124 | reinterpret_cast<int32_t*>(&uid))) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 125 | return ok(); | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 126 | } else { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 127 | return exception(binder::Status::EX_SECURITY, | 
|  | 128 | StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 129 | } | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | binder::Status checkUid(uid_t expectedUid) { | 
|  | 133 | uid_t uid = IPCThreadState::self()->getCallingUid(); | 
|  | 134 | if (uid == expectedUid || uid == AID_ROOT) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 135 | return ok(); | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 136 | } else { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 137 | return exception(binder::Status::EX_SECURITY, | 
|  | 138 | StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | binder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { | 
|  | 143 | if (!uuid || is_valid_filename(*uuid)) { | 
|  | 144 | return ok(); | 
|  | 145 | } else { | 
|  | 146 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, | 
|  | 147 | StringPrintf("UUID %s is malformed", uuid->c_str())); | 
|  | 148 | } | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | binder::Status checkArgumentPackageName(const std::string& packageName) { | 
|  | 152 | if (is_valid_package_name(packageName.c_str())) { | 
|  | 153 | return ok(); | 
|  | 154 | } else { | 
|  | 155 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, | 
|  | 156 | StringPrintf("Package name %s is malformed", packageName.c_str())); | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 157 | } | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | #define ENFORCE_UID(uid) {                                  \ | 
|  | 161 | binder::Status status = checkUid((uid));                \ | 
|  | 162 | if (!status.isOk()) {                                   \ | 
|  | 163 | return status;                                      \ | 
|  | 164 | }                                                       \ | 
|  | 165 | } | 
|  | 166 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 167 | #define CHECK_ARGUMENT_UUID(uuid) {                         \ | 
|  | 168 | binder::Status status = checkArgumentUuid((uuid));      \ | 
|  | 169 | if (!status.isOk()) {                                   \ | 
|  | 170 | return status;                                      \ | 
|  | 171 | }                                                       \ | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | #define CHECK_ARGUMENT_PACKAGE_NAME(packageName) {          \ | 
|  | 175 | binder::Status status =                                 \ | 
|  | 176 | checkArgumentPackageName((packageName));        \ | 
|  | 177 | if (!status.isOk()) {                                   \ | 
|  | 178 | return status;                                      \ | 
|  | 179 | }                                                       \ | 
|  | 180 | } | 
|  | 181 |  | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 182 | }  // namespace | 
|  | 183 |  | 
|  | 184 | status_t InstalldNativeService::start() { | 
|  | 185 | IPCThreadState::self()->disableBackgroundScheduling(true); | 
|  | 186 | status_t ret = BinderService<InstalldNativeService>::publish(); | 
|  | 187 | if (ret != android::OK) { | 
|  | 188 | return ret; | 
|  | 189 | } | 
|  | 190 | sp<ProcessState> ps(ProcessState::self()); | 
|  | 191 | ps->startThreadPool(); | 
|  | 192 | ps->giveThreadPoolName(); | 
|  | 193 | return android::OK; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | status_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 197 | auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd)); | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 198 | const binder::Status dump_permission = checkPermission(kDump); | 
|  | 199 | if (!dump_permission.isOk()) { | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 200 | out << dump_permission.toString8() << endl; | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 201 | return PERMISSION_DENIED; | 
|  | 202 | } | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 203 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 204 |  | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 205 | out << "installd is happy!" << endl; | 
|  | 206 |  | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 207 | { | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 208 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); | 
|  | 209 | out << endl << "Storage mounts:" << endl; | 
|  | 210 | for (const auto& n : mStorageMounts) { | 
|  | 211 | out << "    " << n.first << " = " << n.second << endl; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | out << endl << "Quota reverse mounts:" << endl; | 
|  | 215 | for (const auto& n : mQuotaReverseMounts) { | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 216 | out << "    " << n.first << " = " << n.second << endl; | 
|  | 217 | } | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 218 | } | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 219 |  | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 220 | { | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 221 | std::lock_guard<std::recursive_mutex> lock(mQuotasLock); | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 222 | out << endl << "Per-UID cache quotas:" << endl; | 
|  | 223 | for (const auto& n : mCacheQuotas) { | 
|  | 224 | out << "    " << n.first << " = " << n.second << endl; | 
|  | 225 | } | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 228 | out << endl; | 
|  | 229 | out.flush(); | 
|  | 230 |  | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 231 | return NO_ERROR; | 
|  | 232 | } | 
|  | 233 |  | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 234 | /** | 
|  | 235 | * Perform restorecon of the given path, but only perform recursive restorecon | 
|  | 236 | * if the label of that top-level file actually changed.  This can save us | 
|  | 237 | * significant time by avoiding no-op traversals of large filesystem trees. | 
|  | 238 | */ | 
| Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 239 | static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid, | 
|  | 240 | bool existing) { | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 241 | int res = 0; | 
|  | 242 | char* before = nullptr; | 
|  | 243 | char* after = nullptr; | 
|  | 244 |  | 
|  | 245 | // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by | 
|  | 246 | // libselinux. Not needed here. | 
|  | 247 |  | 
| Jeff Sharkey | 8567ebf | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 248 | if (lgetfilecon(path.c_str(), &before) < 0) { | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 249 | PLOG(ERROR) << "Failed before getfilecon for " << path; | 
|  | 250 | goto fail; | 
|  | 251 | } | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 252 | if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 253 | PLOG(ERROR) << "Failed top-level restorecon for " << path; | 
|  | 254 | goto fail; | 
|  | 255 | } | 
| Jeff Sharkey | 8567ebf | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 256 | if (lgetfilecon(path.c_str(), &after) < 0) { | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 257 | PLOG(ERROR) << "Failed after getfilecon for " << path; | 
|  | 258 | goto fail; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | // If the initial top-level restorecon above changed the label, then go | 
|  | 262 | // back and restorecon everything recursively | 
|  | 263 | if (strcmp(before, after)) { | 
| Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 264 | if (existing) { | 
|  | 265 | LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " | 
|  | 266 | << path << "; running recursive restorecon"; | 
|  | 267 | } | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 268 | if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 269 | SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { | 
|  | 270 | PLOG(ERROR) << "Failed recursive restorecon for " << path; | 
|  | 271 | goto fail; | 
|  | 272 | } | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | goto done; | 
|  | 276 | fail: | 
|  | 277 | res = -1; | 
|  | 278 | done: | 
|  | 279 | free(before); | 
|  | 280 | free(after); | 
|  | 281 | return res; | 
|  | 282 | } | 
|  | 283 |  | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 284 | static int restorecon_app_data_lazy(const std::string& parent, const char* name, | 
| Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 285 | const std::string& seInfo, uid_t uid, bool existing) { | 
|  | 286 | return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid, | 
|  | 287 | existing); | 
| Jeff Sharkey | 8567ebf | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 288 | } | 
|  | 289 |  | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 290 | static int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { | 
| Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 291 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { | 
|  | 292 | PLOG(ERROR) << "Failed to prepare " << path; | 
|  | 293 | return -1; | 
|  | 294 | } | 
| Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 295 | return 0; | 
|  | 296 | } | 
|  | 297 |  | 
| Jeff Sharkey | e59c85c | 2017-04-02 21:53:14 -0600 | [diff] [blame] | 298 | /** | 
|  | 299 | * Ensure that we have a hard-limit quota to protect against abusive apps; | 
|  | 300 | * they should never use more than 90% of blocks or 50% of inodes. | 
|  | 301 | */ | 
|  | 302 | static int prepare_app_quota(const std::unique_ptr<std::string>& uuid, const std::string& device, | 
|  | 303 | uid_t uid) { | 
|  | 304 | if (device.empty()) return 0; | 
|  | 305 |  | 
|  | 306 | struct dqblk dq; | 
|  | 307 | if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, | 
|  | 308 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 309 | PLOG(WARNING) << "Failed to find quota for " << uid; | 
|  | 310 | return -1; | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | if ((dq.dqb_bhardlimit == 0) || (dq.dqb_ihardlimit == 0)) { | 
|  | 314 | auto path = create_data_path(uuid ? uuid->c_str() : nullptr); | 
|  | 315 | struct statvfs stat; | 
|  | 316 | if (statvfs(path.c_str(), &stat) != 0) { | 
|  | 317 | PLOG(WARNING) << "Failed to statvfs " << path; | 
|  | 318 | return -1; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | dq.dqb_valid = QIF_LIMITS; | 
|  | 322 | dq.dqb_bhardlimit = (((stat.f_blocks * stat.f_frsize) / 10) * 9) / QIF_DQBLKSIZE; | 
|  | 323 | dq.dqb_ihardlimit = (stat.f_files / 2); | 
|  | 324 | if (quotactl(QCMD(Q_SETQUOTA, USRQUOTA), device.c_str(), uid, | 
|  | 325 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 326 | PLOG(WARNING) << "Failed to set hard quota for " << uid; | 
|  | 327 | return -1; | 
|  | 328 | } else { | 
|  | 329 | LOG(DEBUG) << "Applied hard quotas for " << uid; | 
|  | 330 | return 0; | 
|  | 331 | } | 
|  | 332 | } else { | 
|  | 333 | // Hard quota already set; assume it's reasonable | 
|  | 334 | return 0; | 
|  | 335 | } | 
|  | 336 | } | 
|  | 337 |  | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 338 | binder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, | 
|  | 339 | const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, | 
| Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 340 | const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 341 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 342 | CHECK_ARGUMENT_UUID(uuid); | 
|  | 343 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 344 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 345 |  | 
|  | 346 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 347 | const char* pkgname = packageName.c_str(); | 
|  | 348 |  | 
| Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 349 | // Assume invalid inode unless filled in below | 
|  | 350 | if (_aidl_return != nullptr) *_aidl_return = -1; | 
|  | 351 |  | 
| Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame] | 352 | int32_t uid = multiuser_get_uid(userId, appId); | 
|  | 353 | int32_t cacheGid = multiuser_get_cache_gid(userId, appId); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 354 | mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; | 
|  | 355 |  | 
| Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame] | 356 | // If UID doesn't have a specific cache GID, use UID value | 
|  | 357 | if (cacheGid == -1) { | 
|  | 358 | cacheGid = uid; | 
|  | 359 | } | 
|  | 360 |  | 
| Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 361 | if (flags & FLAG_STORAGE_CE) { | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 362 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); | 
| Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 363 | bool existing = (access(path.c_str(), F_OK) == 0); | 
|  | 364 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 365 | if (prepare_app_dir(path, targetMode, uid) || | 
|  | 366 | prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || | 
|  | 367 | prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 368 | return error("Failed to prepare " + path); | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 369 | } | 
|  | 370 |  | 
|  | 371 | // Consider restorecon over contents if label changed | 
| Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 372 | if (restorecon_app_data_lazy(path, seInfo, uid, existing) || | 
|  | 373 | restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || | 
|  | 374 | restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 375 | return error("Failed to restorecon " + path); | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 376 | } | 
| Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 377 |  | 
|  | 378 | // Remember inode numbers of cache directories so that we can clear | 
|  | 379 | // contents while CE storage is locked | 
|  | 380 | if (write_path_inode(path, "cache", kXattrInodeCache) || | 
|  | 381 | write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 382 | return error("Failed to write_path_inode for " + path); | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 383 | } | 
| Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 384 |  | 
|  | 385 | // And return the CE inode of the top-level data directory so we can | 
|  | 386 | // clear contents while CE storage is locked | 
|  | 387 | if ((_aidl_return != nullptr) | 
|  | 388 | && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) { | 
|  | 389 | return error("Failed to get_path_inode for " + path); | 
|  | 390 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 391 | } | 
| Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 392 | if (flags & FLAG_STORAGE_DE) { | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 393 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); | 
| Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 394 | bool existing = (access(path.c_str(), F_OK) == 0); | 
|  | 395 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 396 | if (prepare_app_dir(path, targetMode, uid) || | 
|  | 397 | prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || | 
|  | 398 | prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 399 | return error("Failed to prepare " + path); | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 400 | } | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 401 |  | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 402 | // Consider restorecon over contents if label changed | 
| Jeff Sharkey | 7b6c840 | 2017-04-15 12:09:22 -0600 | [diff] [blame] | 403 | if (restorecon_app_data_lazy(path, seInfo, uid, existing) || | 
|  | 404 | restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || | 
|  | 405 | restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 406 | return error("Failed to restorecon " + path); | 
| Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 407 | } | 
|  | 408 |  | 
| Jeff Sharkey | e59c85c | 2017-04-02 21:53:14 -0600 | [diff] [blame] | 409 | if (prepare_app_quota(uuid, findQuotaDeviceForUuid(uuid), uid)) { | 
|  | 410 | return error("Failed to set hard quota " + path); | 
|  | 411 | } | 
|  | 412 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 413 | if (property_get_bool("dalvik.vm.usejitprofiles", false)) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 414 | const std::string profile_dir = | 
|  | 415 | create_primary_current_profile_package_dir_path(userId, pkgname); | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 416 | // read-write-execute only for the app user. | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 417 | if (fs_prepare_dir_strict(profile_dir.c_str(), 0700, uid, uid) != 0) { | 
|  | 418 | return error("Failed to prepare " + profile_dir); | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 419 | } | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 420 | const std::string profile_file = create_current_profile_path(userId, pkgname, | 
|  | 421 | /*is_secondary_dex*/false); | 
| Calin Juravle | bc56e7d | 2016-05-24 15:33:12 +0100 | [diff] [blame] | 422 | // read-write only for the app user. | 
|  | 423 | if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 424 | return error("Failed to prepare " + profile_file); | 
| Calin Juravle | bc56e7d | 2016-05-24 15:33:12 +0100 | [diff] [blame] | 425 | } | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 426 | const std::string ref_profile_path = | 
|  | 427 | create_primary_reference_profile_package_dir_path(pkgname); | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 428 | // dex2oat/profman runs under the shared app gid and it needs to read/write reference | 
|  | 429 | // profiles. | 
| Jeff Sharkey | adddd98 | 2017-01-03 14:33:50 -0700 | [diff] [blame] | 430 | int shared_app_gid = multiuser_get_shared_gid(0, appId); | 
| Jeff Sharkey | 2e6dc9b | 2016-12-15 14:50:11 -0700 | [diff] [blame] | 431 | if ((shared_app_gid != -1) && fs_prepare_dir_strict( | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 432 | ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 433 | return error("Failed to prepare " + ref_profile_path); | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 434 | } | 
|  | 435 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 436 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 437 | return ok(); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 438 | } | 
|  | 439 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 440 | binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, | 
|  | 441 | const std::string& packageName, int32_t userId, int32_t flags) { | 
|  | 442 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 443 | CHECK_ARGUMENT_UUID(uuid); | 
|  | 444 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 445 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 446 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 447 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 448 | const char* pkgname = packageName.c_str(); | 
|  | 449 |  | 
| Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 450 | // This method only exists to upgrade system apps that have requested | 
|  | 451 | // forceDeviceEncrypted, so their default storage always lives in a | 
|  | 452 | // consistent location.  This only works on non-FBE devices, since we | 
|  | 453 | // never want to risk exposing data on a device with real CE/DE storage. | 
|  | 454 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 455 | auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); | 
|  | 456 | auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); | 
| Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 457 |  | 
|  | 458 | // If neither directory is marked as default, assume CE is default | 
|  | 459 | if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 | 
|  | 460 | && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { | 
|  | 461 | if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 462 | return error("Failed to mark default storage " + ce_path); | 
| Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 463 | } | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | // Migrate default data location if needed | 
|  | 467 | auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; | 
|  | 468 | auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; | 
|  | 469 |  | 
|  | 470 | if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { | 
|  | 471 | LOG(WARNING) << "Requested default storage " << target | 
|  | 472 | << " is not active; migrating from " << source; | 
|  | 473 | if (delete_dir_contents_and_dir(target) != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 474 | return error("Failed to delete " + target); | 
| Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 475 | } | 
|  | 476 | if (rename(source.c_str(), target.c_str()) != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 477 | return error("Failed to rename " + source + " to " + target); | 
| Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 478 | } | 
|  | 479 | } | 
|  | 480 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 481 | return ok(); | 
| Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 482 | } | 
|  | 483 |  | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 484 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 485 | binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { | 
|  | 486 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 487 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 488 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 489 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 490 | binder::Status res = ok(); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 491 | if (!clear_primary_reference_profile(packageName)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 492 | res = error("Failed to clear reference profile for " + packageName); | 
|  | 493 | } | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 494 | if (!clear_primary_current_profiles(packageName)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 495 | res = error("Failed to clear current profiles for " + packageName); | 
|  | 496 | } | 
|  | 497 | return res; | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 498 | } | 
|  | 499 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 500 | binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, | 
|  | 501 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { | 
|  | 502 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 503 | CHECK_ARGUMENT_UUID(uuid); | 
|  | 504 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 505 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 506 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 507 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 508 | const char* pkgname = packageName.c_str(); | 
|  | 509 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 510 | binder::Status res = ok(); | 
| Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 511 | if (flags & FLAG_STORAGE_CE) { | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 512 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); | 
| Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 513 | if (flags & FLAG_CLEAR_CACHE_ONLY) { | 
|  | 514 | path = read_path_inode(path, "cache", kXattrInodeCache); | 
|  | 515 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { | 
|  | 516 | path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); | 
|  | 517 | } | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 518 | if (access(path.c_str(), F_OK) == 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 519 | if (delete_dir_contents(path) != 0) { | 
|  | 520 | res = error("Failed to delete contents of " + path); | 
|  | 521 | } | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 522 | } | 
|  | 523 | } | 
| Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 524 | if (flags & FLAG_STORAGE_DE) { | 
| Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 525 | std::string suffix = ""; | 
|  | 526 | bool only_cache = false; | 
|  | 527 | if (flags & FLAG_CLEAR_CACHE_ONLY) { | 
|  | 528 | suffix = CACHE_DIR_POSTFIX; | 
|  | 529 | only_cache = true; | 
|  | 530 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { | 
|  | 531 | suffix = CODE_CACHE_DIR_POSTFIX; | 
|  | 532 | only_cache = true; | 
|  | 533 | } | 
|  | 534 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 535 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 536 | if (access(path.c_str(), F_OK) == 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 537 | if (delete_dir_contents(path) != 0) { | 
|  | 538 | res = error("Failed to delete contents of " + path); | 
|  | 539 | } | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 540 | } | 
| Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 541 | if (!only_cache) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 542 | if (!clear_primary_current_profile(packageName, userId)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 543 | res = error("Failed to clear current profile for " + packageName); | 
| Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 544 | } | 
|  | 545 | } | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 546 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 547 | return res; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 548 | } | 
|  | 549 |  | 
| Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 550 | static int destroy_app_reference_profile(const std::string& pkgname) { | 
| Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 551 | return delete_dir_contents_and_dir( | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 552 | create_primary_reference_profile_package_dir_path(pkgname), | 
| Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 553 | /*ignore_if_missing*/ true); | 
|  | 554 | } | 
|  | 555 |  | 
| Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 556 | static int destroy_app_current_profiles(const std::string& pkgname, userid_t userid) { | 
| Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 557 | return delete_dir_contents_and_dir( | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 558 | create_primary_current_profile_package_dir_path(userid, pkgname), | 
| Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 559 | /*ignore_if_missing*/ true); | 
| Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 560 | } | 
|  | 561 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 562 | binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { | 
|  | 563 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 564 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 565 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 566 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 567 | binder::Status res = ok(); | 
| Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 568 | std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); | 
|  | 569 | for (auto user : users) { | 
| Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 570 | if (destroy_app_current_profiles(packageName, user) != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 571 | res = error("Failed to destroy current profiles for " + packageName); | 
|  | 572 | } | 
| Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 573 | } | 
| Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 574 | if (destroy_app_reference_profile(packageName) != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 575 | res = error("Failed to destroy reference profile for " + packageName); | 
|  | 576 | } | 
|  | 577 | return res; | 
| Calin Juravle | caa6b80 | 2016-03-22 14:15:52 +0000 | [diff] [blame] | 578 | } | 
|  | 579 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 580 | binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, | 
|  | 581 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { | 
|  | 582 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 583 | CHECK_ARGUMENT_UUID(uuid); | 
|  | 584 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 585 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 586 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 587 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 588 | const char* pkgname = packageName.c_str(); | 
|  | 589 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 590 | binder::Status res = ok(); | 
| Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 591 | if (flags & FLAG_STORAGE_CE) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 592 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); | 
|  | 593 | if (delete_dir_contents_and_dir(path) != 0) { | 
|  | 594 | res = error("Failed to delete " + path); | 
|  | 595 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 596 | } | 
| Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 597 | if (flags & FLAG_STORAGE_DE) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 598 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); | 
|  | 599 | if (delete_dir_contents_and_dir(path) != 0) { | 
|  | 600 | res = error("Failed to delete " + path); | 
|  | 601 | } | 
| Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 602 | destroy_app_current_profiles(packageName, userId); | 
| Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 603 | // TODO(calin): If the package is still installed by other users it's probably | 
|  | 604 | // beneficial to keep the reference profile around. | 
|  | 605 | // Verify if it's ok to do that. | 
| Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 606 | destroy_app_reference_profile(packageName); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 607 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 608 | return res; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 609 | } | 
|  | 610 |  | 
| Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 611 | static gid_t get_cache_gid(uid_t uid) { | 
|  | 612 | int32_t gid = multiuser_get_cache_gid(multiuser_get_user_id(uid), multiuser_get_app_id(uid)); | 
|  | 613 | return (gid != -1) ? gid : uid; | 
|  | 614 | } | 
|  | 615 |  | 
|  | 616 | binder::Status InstalldNativeService::fixupAppData(const std::unique_ptr<std::string>& uuid, | 
|  | 617 | int32_t flags) { | 
|  | 618 | ENFORCE_UID(AID_SYSTEM); | 
|  | 619 | CHECK_ARGUMENT_UUID(uuid); | 
|  | 620 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
|  | 621 |  | 
|  | 622 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 623 | for (auto user : get_known_users(uuid_)) { | 
|  | 624 | ATRACE_BEGIN("fixup user"); | 
|  | 625 | FTS* fts; | 
|  | 626 | FTSENT* p; | 
| Jeff Sharkey | d9ff77f | 2017-04-14 18:54:49 -0600 | [diff] [blame] | 627 | auto ce_path = create_data_user_ce_path(uuid_, user); | 
|  | 628 | auto de_path = create_data_user_de_path(uuid_, user); | 
|  | 629 | char *argv[] = { (char*) ce_path.c_str(), (char*) de_path.c_str(), nullptr }; | 
| Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 630 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { | 
|  | 631 | return error("Failed to fts_open"); | 
|  | 632 | } | 
|  | 633 | while ((p = fts_read(fts)) != nullptr) { | 
|  | 634 | if (p->fts_info == FTS_D && p->fts_level == 1) { | 
|  | 635 | // Track down inodes of cache directories | 
|  | 636 | uint64_t raw = 0; | 
|  | 637 | ino_t inode_cache = 0; | 
|  | 638 | ino_t inode_code_cache = 0; | 
|  | 639 | if (getxattr(p->fts_path, kXattrInodeCache, &raw, sizeof(raw)) == sizeof(raw)) { | 
|  | 640 | inode_cache = raw; | 
|  | 641 | } | 
|  | 642 | if (getxattr(p->fts_path, kXattrInodeCodeCache, &raw, sizeof(raw)) == sizeof(raw)) { | 
|  | 643 | inode_code_cache = raw; | 
|  | 644 | } | 
|  | 645 |  | 
|  | 646 | // Figure out expected GID of each child | 
|  | 647 | FTSENT* child = fts_children(fts, 0); | 
|  | 648 | while (child != nullptr) { | 
|  | 649 | if ((child->fts_statp->st_ino == inode_cache) | 
|  | 650 | || (child->fts_statp->st_ino == inode_code_cache) | 
|  | 651 | || !strcmp(child->fts_name, "cache") | 
|  | 652 | || !strcmp(child->fts_name, "code_cache")) { | 
|  | 653 | child->fts_number = get_cache_gid(p->fts_statp->st_uid); | 
|  | 654 | } else { | 
|  | 655 | child->fts_number = p->fts_statp->st_uid; | 
|  | 656 | } | 
|  | 657 | child = child->fts_link; | 
|  | 658 | } | 
|  | 659 | } else if (p->fts_level >= 2) { | 
|  | 660 | if (p->fts_level > 2) { | 
|  | 661 | // Inherit GID from parent once we're deeper into tree | 
|  | 662 | p->fts_number = p->fts_parent->fts_number; | 
|  | 663 | } | 
|  | 664 |  | 
|  | 665 | uid_t uid = p->fts_parent->fts_statp->st_uid; | 
|  | 666 | gid_t cache_gid = get_cache_gid(uid); | 
|  | 667 | gid_t expected = p->fts_number; | 
|  | 668 | gid_t actual = p->fts_statp->st_gid; | 
|  | 669 | if (actual == expected) { | 
|  | 670 | #if FIXUP_DEBUG | 
|  | 671 | LOG(DEBUG) << "Ignoring " << p->fts_path << " with expected GID " << expected; | 
|  | 672 | #endif | 
|  | 673 | if (!(flags & FLAG_FORCE)) { | 
|  | 674 | fts_set(fts, p, FTS_SKIP); | 
|  | 675 | } | 
|  | 676 | } else if ((actual == uid) || (actual == cache_gid)) { | 
|  | 677 | // Only consider fixing up when current GID belongs to app | 
|  | 678 | if (p->fts_info != FTS_D) { | 
|  | 679 | LOG(INFO) << "Fixing " << p->fts_path << " with unexpected GID " << actual | 
|  | 680 | << " instead of " << expected; | 
|  | 681 | } | 
|  | 682 | switch (p->fts_info) { | 
|  | 683 | case FTS_DP: | 
|  | 684 | // If we're moving towards cache GID, we need to set S_ISGID | 
|  | 685 | if (expected == cache_gid) { | 
|  | 686 | if (chmod(p->fts_path, 02771) != 0) { | 
|  | 687 | PLOG(WARNING) << "Failed to chmod " << p->fts_path; | 
|  | 688 | } | 
|  | 689 | } | 
|  | 690 | // Intentional fall through to also set GID | 
|  | 691 | case FTS_F: | 
|  | 692 | if (chown(p->fts_path, -1, expected) != 0) { | 
|  | 693 | PLOG(WARNING) << "Failed to chown " << p->fts_path; | 
|  | 694 | } | 
|  | 695 | break; | 
|  | 696 | case FTS_SL: | 
|  | 697 | case FTS_SLNONE: | 
|  | 698 | if (lchown(p->fts_path, -1, expected) != 0) { | 
|  | 699 | PLOG(WARNING) << "Failed to chown " << p->fts_path; | 
|  | 700 | } | 
|  | 701 | break; | 
|  | 702 | } | 
|  | 703 | } else { | 
|  | 704 | // Ignore all other GID transitions, since they're kinda shady | 
|  | 705 | LOG(WARNING) << "Ignoring " << p->fts_path << " with unexpected GID " << actual | 
|  | 706 | << " instead of " << expected; | 
|  | 707 | } | 
|  | 708 | } | 
|  | 709 | } | 
|  | 710 | fts_close(fts); | 
|  | 711 | ATRACE_END(); | 
|  | 712 | } | 
|  | 713 | return ok(); | 
|  | 714 | } | 
|  | 715 |  | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 716 | binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, | 
|  | 717 | const std::unique_ptr<std::string>& toUuid, const std::string& packageName, | 
|  | 718 | const std::string& dataAppName, int32_t appId, const std::string& seInfo, | 
|  | 719 | int32_t targetSdkVersion) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 720 | ENFORCE_UID(AID_SYSTEM); | 
|  | 721 | CHECK_ARGUMENT_UUID(fromUuid); | 
|  | 722 | CHECK_ARGUMENT_UUID(toUuid); | 
|  | 723 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 724 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 725 |  | 
|  | 726 | const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; | 
|  | 727 | const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; | 
|  | 728 | const char* package_name = packageName.c_str(); | 
|  | 729 | const char* data_app_name = dataAppName.c_str(); | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 730 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 731 | binder::Status res = ok(); | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 732 | std::vector<userid_t> users = get_known_users(from_uuid); | 
|  | 733 |  | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 734 | // Copy app | 
|  | 735 | { | 
| Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 736 | auto from = create_data_app_package_path(from_uuid, data_app_name); | 
|  | 737 | auto to = create_data_app_package_path(to_uuid, data_app_name); | 
|  | 738 | auto to_parent = create_data_app_path(to_uuid); | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 739 |  | 
|  | 740 | char *argv[] = { | 
|  | 741 | (char*) kCpPath, | 
|  | 742 | (char*) "-F", /* delete any existing destination file first (--remove-destination) */ | 
|  | 743 | (char*) "-p", /* preserve timestamps, ownership, and permissions */ | 
|  | 744 | (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ | 
|  | 745 | (char*) "-P", /* Do not follow symlinks [default] */ | 
|  | 746 | (char*) "-d", /* don't dereference symlinks */ | 
|  | 747 | (char*) from.c_str(), | 
|  | 748 | (char*) to_parent.c_str() | 
|  | 749 | }; | 
|  | 750 |  | 
|  | 751 | LOG(DEBUG) << "Copying " << from << " to " << to; | 
|  | 752 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 753 | if (rc != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 754 | res = error(rc, "Failed copying " + from + " to " + to); | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 755 | goto fail; | 
|  | 756 | } | 
|  | 757 |  | 
|  | 758 | if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 759 | res = error("Failed to restorecon " + to); | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 760 | goto fail; | 
|  | 761 | } | 
|  | 762 | } | 
|  | 763 |  | 
|  | 764 | // Copy private data for all known users | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 765 | for (auto user : users) { | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 766 |  | 
|  | 767 | // Data source may not exist for all users; that's okay | 
| Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 768 | auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); | 
|  | 769 | if (access(from_ce.c_str(), F_OK) != 0) { | 
|  | 770 | LOG(INFO) << "Missing source " << from_ce; | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 771 | continue; | 
|  | 772 | } | 
|  | 773 |  | 
| Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 774 | if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, | 
| Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 775 | seInfo, targetSdkVersion, nullptr).isOk()) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 776 | res = error("Failed to create package target"); | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 777 | goto fail; | 
|  | 778 | } | 
|  | 779 |  | 
|  | 780 | char *argv[] = { | 
|  | 781 | (char*) kCpPath, | 
|  | 782 | (char*) "-F", /* delete any existing destination file first (--remove-destination) */ | 
|  | 783 | (char*) "-p", /* preserve timestamps, ownership, and permissions */ | 
|  | 784 | (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ | 
|  | 785 | (char*) "-P", /* Do not follow symlinks [default] */ | 
|  | 786 | (char*) "-d", /* don't dereference symlinks */ | 
| Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 787 | nullptr, | 
|  | 788 | nullptr | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 789 | }; | 
|  | 790 |  | 
| Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 791 | { | 
|  | 792 | auto from = create_data_user_de_package_path(from_uuid, user, package_name); | 
|  | 793 | auto to = create_data_user_de_path(to_uuid, user); | 
|  | 794 | argv[6] = (char*) from.c_str(); | 
|  | 795 | argv[7] = (char*) to.c_str(); | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 796 |  | 
| Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 797 | LOG(DEBUG) << "Copying " << from << " to " << to; | 
|  | 798 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); | 
|  | 799 | if (rc != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 800 | res = error(rc, "Failed copying " + from + " to " + to); | 
| Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 801 | goto fail; | 
|  | 802 | } | 
|  | 803 | } | 
|  | 804 | { | 
|  | 805 | auto from = create_data_user_ce_package_path(from_uuid, user, package_name); | 
|  | 806 | auto to = create_data_user_ce_path(to_uuid, user); | 
|  | 807 | argv[6] = (char*) from.c_str(); | 
|  | 808 | argv[7] = (char*) to.c_str(); | 
|  | 809 |  | 
|  | 810 | LOG(DEBUG) << "Copying " << from << " to " << to; | 
|  | 811 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); | 
|  | 812 | if (rc != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 813 | res = error(rc, "Failed copying " + from + " to " + to); | 
| Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 814 | goto fail; | 
|  | 815 | } | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 816 | } | 
|  | 817 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 818 | if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, | 
|  | 819 | appId, seInfo).isOk()) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 820 | res = error("Failed to restorecon"); | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 821 | goto fail; | 
|  | 822 | } | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 823 | } | 
|  | 824 |  | 
| Jeff Sharkey | 31f0898 | 2015-07-07 13:31:37 -0700 | [diff] [blame] | 825 | // We let the framework scan the new location and persist that before | 
|  | 826 | // deleting the data in the old location; this ordering ensures that | 
|  | 827 | // we can recover from things like battery pulls. | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 828 | return ok(); | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 829 |  | 
|  | 830 | fail: | 
|  | 831 | // Nuke everything we might have already copied | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 832 | { | 
| Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 833 | auto to = create_data_app_package_path(to_uuid, data_app_name); | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 834 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { | 
|  | 835 | LOG(WARNING) << "Failed to rollback " << to; | 
|  | 836 | } | 
|  | 837 | } | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 838 | for (auto user : users) { | 
| Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 839 | { | 
|  | 840 | auto to = create_data_user_de_package_path(to_uuid, user, package_name); | 
|  | 841 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { | 
|  | 842 | LOG(WARNING) << "Failed to rollback " << to; | 
|  | 843 | } | 
|  | 844 | } | 
|  | 845 | { | 
|  | 846 | auto to = create_data_user_ce_package_path(to_uuid, user, package_name); | 
|  | 847 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { | 
|  | 848 | LOG(WARNING) << "Failed to rollback " << to; | 
|  | 849 | } | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 850 | } | 
|  | 851 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 852 | return res; | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 853 | } | 
|  | 854 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 855 | binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, | 
|  | 856 | int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { | 
|  | 857 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 858 | CHECK_ARGUMENT_UUID(uuid); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 859 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 860 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 861 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
| Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 862 | if (flags & FLAG_STORAGE_DE) { | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 863 | if (uuid_ == nullptr) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 864 | if (ensure_config_user_dirs(userId) != 0) { | 
| Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 865 | return error(StringPrintf("Failed to ensure dirs for %d", userId)); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 866 | } | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 867 | } | 
|  | 868 | } | 
| Jeff Sharkey | e59c85c | 2017-04-02 21:53:14 -0600 | [diff] [blame] | 869 |  | 
|  | 870 | // Data under /data/media doesn't have an app, but we still want | 
|  | 871 | // to limit it to prevent abuse. | 
|  | 872 | if (prepare_app_quota(uuid, findQuotaDeviceForUuid(uuid), | 
|  | 873 | multiuser_get_uid(userId, AID_MEDIA_RW))) { | 
|  | 874 | return error("Failed to set hard quota for media_rw"); | 
|  | 875 | } | 
|  | 876 |  | 
| Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 877 | return ok(); | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 878 | } | 
|  | 879 |  | 
|  | 880 | binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, | 
|  | 881 | int32_t userId, int32_t flags) { | 
|  | 882 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 883 | CHECK_ARGUMENT_UUID(uuid); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 884 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 885 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 886 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 887 | binder::Status res = ok(); | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 888 | if (flags & FLAG_STORAGE_DE) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 889 | auto path = create_data_user_de_path(uuid_, userId); | 
|  | 890 | if (delete_dir_contents_and_dir(path, true) != 0) { | 
|  | 891 | res = error("Failed to delete " + path); | 
|  | 892 | } | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 893 | if (uuid_ == nullptr) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 894 | path = create_data_misc_legacy_path(userId); | 
|  | 895 | if (delete_dir_contents_and_dir(path, true) != 0) { | 
|  | 896 | res = error("Failed to delete " + path); | 
|  | 897 | } | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 898 | path = create_primary_cur_profile_dir_path(userId); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 899 | if (delete_dir_contents_and_dir(path, true) != 0) { | 
|  | 900 | res = error("Failed to delete " + path); | 
|  | 901 | } | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 902 | } | 
| Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 903 | } | 
| Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 904 | if (flags & FLAG_STORAGE_CE) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 905 | auto path = create_data_user_ce_path(uuid_, userId); | 
|  | 906 | if (delete_dir_contents_and_dir(path, true) != 0) { | 
|  | 907 | res = error("Failed to delete " + path); | 
|  | 908 | } | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 909 | path = findDataMediaPath(uuid, userId); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 910 | if (delete_dir_contents_and_dir(path, true) != 0) { | 
|  | 911 | res = error("Failed to delete " + path); | 
|  | 912 | } | 
| Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 913 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 914 | return res; | 
| Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 915 | } | 
|  | 916 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 917 | /* Try to ensure free_size bytes of storage are available. | 
|  | 918 | * Returns 0 on success. | 
|  | 919 | * This is rather simple-minded because doing a full LRU would | 
|  | 920 | * be potentially memory-intensive, and without atime it would | 
|  | 921 | * also require that apps constantly modify file metadata even | 
|  | 922 | * when just reading from the cache, which is pretty awful. | 
|  | 923 | */ | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 924 | binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 925 | int64_t freeStorageSize, int32_t flags) { | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 926 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 927 | CHECK_ARGUMENT_UUID(uuid); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 928 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 929 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 930 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 931 | auto data_path = create_data_path(uuid_); | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 932 | auto device = findQuotaDeviceForUuid(uuid); | 
|  | 933 | auto noop = (flags & FLAG_FREE_CACHE_NOOP); | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 934 |  | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 935 | int64_t free = data_disk_free(data_path); | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 936 | if (free < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 937 | return error("Failed to determine free space for " + data_path); | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 938 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 939 |  | 
| Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 940 | int64_t needed = freeStorageSize - free; | 
|  | 941 | LOG(DEBUG) << "Device " << data_path << " has " << free << " free; requested " | 
|  | 942 | << freeStorageSize << "; needed " << needed; | 
|  | 943 |  | 
|  | 944 | if (free >= freeStorageSize) { | 
|  | 945 | return ok(); | 
|  | 946 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 947 |  | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 948 | if (flags & FLAG_FREE_CACHE_V2) { | 
|  | 949 | // This new cache strategy fairly removes files from UIDs by deleting | 
|  | 950 | // files from the UIDs which are most over their allocated quota | 
|  | 951 |  | 
|  | 952 | // 1. Create trackers for every known UID | 
|  | 953 | ATRACE_BEGIN("create"); | 
|  | 954 | std::unordered_map<uid_t, std::shared_ptr<CacheTracker>> trackers; | 
|  | 955 | for (auto user : get_known_users(uuid_)) { | 
|  | 956 | FTS *fts; | 
|  | 957 | FTSENT *p; | 
| Jeff Sharkey | d9ff77f | 2017-04-14 18:54:49 -0600 | [diff] [blame] | 958 | auto ce_path = create_data_user_ce_path(uuid_, user); | 
|  | 959 | auto de_path = create_data_user_de_path(uuid_, user); | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 960 | auto media_path = findDataMediaPath(uuid, user) + "/Android/data/"; | 
|  | 961 | char *argv[] = { (char*) ce_path.c_str(), (char*) de_path.c_str(), | 
|  | 962 | (char*) media_path.c_str(), nullptr }; | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 963 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 964 | return error("Failed to fts_open"); | 
|  | 965 | } | 
|  | 966 | while ((p = fts_read(fts)) != NULL) { | 
|  | 967 | if (p->fts_info == FTS_D && p->fts_level == 1) { | 
|  | 968 | uid_t uid = p->fts_statp->st_uid; | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 969 | if (multiuser_get_app_id(uid) == AID_MEDIA_RW) { | 
|  | 970 | uid = (multiuser_get_app_id(p->fts_statp->st_gid) - AID_EXT_GID_START) | 
|  | 971 | + AID_APP_START; | 
|  | 972 | } | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 973 | auto search = trackers.find(uid); | 
|  | 974 | if (search != trackers.end()) { | 
|  | 975 | search->second->addDataPath(p->fts_path); | 
|  | 976 | } else { | 
|  | 977 | auto tracker = std::shared_ptr<CacheTracker>(new CacheTracker( | 
|  | 978 | multiuser_get_user_id(uid), multiuser_get_app_id(uid), device)); | 
|  | 979 | tracker->addDataPath(p->fts_path); | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 980 | { | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 981 | std::lock_guard<std::recursive_mutex> lock(mQuotasLock); | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 982 | tracker->cacheQuota = mCacheQuotas[uid]; | 
|  | 983 | } | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 984 | if (tracker->cacheQuota == 0) { | 
| Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 985 | #if MEASURE_DEBUG | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 986 | LOG(WARNING) << "UID " << uid << " has no cache quota; assuming 64MB"; | 
| Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 987 | #endif | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 988 | tracker->cacheQuota = 67108864; | 
|  | 989 | } | 
|  | 990 | trackers[uid] = tracker; | 
|  | 991 | } | 
|  | 992 | fts_set(fts, p, FTS_SKIP); | 
|  | 993 | } | 
|  | 994 | } | 
|  | 995 | fts_close(fts); | 
|  | 996 | } | 
|  | 997 | ATRACE_END(); | 
|  | 998 |  | 
|  | 999 | // 2. Populate tracker stats and insert into priority queue | 
|  | 1000 | ATRACE_BEGIN("populate"); | 
|  | 1001 | auto cmp = [](std::shared_ptr<CacheTracker> left, std::shared_ptr<CacheTracker> right) { | 
|  | 1002 | return (left->getCacheRatio() < right->getCacheRatio()); | 
|  | 1003 | }; | 
|  | 1004 | std::priority_queue<std::shared_ptr<CacheTracker>, | 
|  | 1005 | std::vector<std::shared_ptr<CacheTracker>>, decltype(cmp)> queue(cmp); | 
|  | 1006 | for (const auto& it : trackers) { | 
|  | 1007 | it.second->loadStats(); | 
|  | 1008 | queue.push(it.second); | 
|  | 1009 | } | 
|  | 1010 | ATRACE_END(); | 
|  | 1011 |  | 
|  | 1012 | // 3. Bounce across the queue, freeing items from whichever tracker is | 
|  | 1013 | // the most over their assigned quota | 
|  | 1014 | ATRACE_BEGIN("bounce"); | 
|  | 1015 | std::shared_ptr<CacheTracker> active; | 
|  | 1016 | while (active || !queue.empty()) { | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 1017 | // Only look at apps under quota when explicitly requested | 
|  | 1018 | if (active && (active->getCacheRatio() < 10000) | 
|  | 1019 | && !(flags & FLAG_FREE_CACHE_V2_DEFY_QUOTA)) { | 
|  | 1020 | LOG(DEBUG) << "Active ratio " << active->getCacheRatio() | 
|  | 1021 | << " isn't over quota, and defy not requested"; | 
|  | 1022 | break; | 
|  | 1023 | } | 
|  | 1024 |  | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1025 | // Find the best tracker to work with; this might involve swapping | 
|  | 1026 | // if the active tracker is no longer the most over quota | 
|  | 1027 | bool nextBetter = active && !queue.empty() | 
|  | 1028 | && active->getCacheRatio() < queue.top()->getCacheRatio(); | 
|  | 1029 | if (!active || nextBetter) { | 
|  | 1030 | if (active) { | 
|  | 1031 | // Current tracker still has items, so we'll consider it | 
|  | 1032 | // again later once it bubbles up to surface | 
|  | 1033 | queue.push(active); | 
|  | 1034 | } | 
|  | 1035 | active = queue.top(); queue.pop(); | 
|  | 1036 | active->ensureItems(); | 
|  | 1037 | continue; | 
|  | 1038 | } | 
|  | 1039 |  | 
|  | 1040 | // If no items remain, go find another tracker | 
|  | 1041 | if (active->items.empty()) { | 
|  | 1042 | active = nullptr; | 
|  | 1043 | continue; | 
|  | 1044 | } else { | 
|  | 1045 | auto item = active->items.back(); | 
|  | 1046 | active->items.pop_back(); | 
|  | 1047 |  | 
|  | 1048 | LOG(DEBUG) << "Purging " << item->toString() << " from " << active->toString(); | 
|  | 1049 | if (!noop) { | 
|  | 1050 | item->purge(); | 
|  | 1051 | } | 
|  | 1052 | active->cacheUsed -= item->size; | 
|  | 1053 | needed -= item->size; | 
|  | 1054 | } | 
|  | 1055 |  | 
|  | 1056 | // Verify that we're actually done before bailing, since sneaky | 
|  | 1057 | // apps might be using hardlinks | 
|  | 1058 | if (needed <= 0) { | 
|  | 1059 | free = data_disk_free(data_path); | 
|  | 1060 | needed = freeStorageSize - free; | 
|  | 1061 | if (needed <= 0) { | 
|  | 1062 | break; | 
|  | 1063 | } else { | 
|  | 1064 | LOG(WARNING) << "Expected to be done but still need " << needed; | 
|  | 1065 | } | 
|  | 1066 | } | 
|  | 1067 | } | 
|  | 1068 | ATRACE_END(); | 
|  | 1069 |  | 
|  | 1070 | } else { | 
| Jeff Sharkey | 1cb4aaf | 2017-03-27 16:41:06 -0600 | [diff] [blame] | 1071 | return error("Legacy cache logic no longer supported"); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1072 | } | 
|  | 1073 |  | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1074 | free = data_disk_free(data_path); | 
|  | 1075 | if (free >= freeStorageSize) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1076 | return ok(); | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1077 | } else { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1078 | return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1079 | freeStorageSize, data_path.c_str(), free)); | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1080 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1081 | } | 
|  | 1082 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1083 | binder::Status InstalldNativeService::rmdex(const std::string& codePath, | 
|  | 1084 | const std::string& instructionSet) { | 
|  | 1085 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1086 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
|  | 1087 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1088 | char dex_path[PKG_PATH_MAX]; | 
|  | 1089 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1090 | const char* path = codePath.c_str(); | 
|  | 1091 | const char* instruction_set = instructionSet.c_str(); | 
|  | 1092 |  | 
| Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 1093 | if (validate_apk_path(path) && validate_system_app_path(path)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1094 | return error("Invalid path " + codePath); | 
| Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 1095 | } | 
|  | 1096 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1097 | if (!create_cache_path(dex_path, path, instruction_set)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1098 | return error("Failed to create cache path for " + codePath); | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1099 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1100 |  | 
|  | 1101 | ALOGV("unlink %s\n", dex_path); | 
|  | 1102 | if (unlink(dex_path) < 0) { | 
| Calin Juravle | 249f2d3 | 2017-05-03 17:22:27 -0700 | [diff] [blame] | 1103 | // It's ok if we don't have a dalvik cache path. Report error only when the path exists | 
|  | 1104 | // but could not be unlinked. | 
|  | 1105 | if (errno != ENOENT) { | 
|  | 1106 | return error(StringPrintf("Failed to unlink %s", dex_path)); | 
|  | 1107 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1108 | } | 
| Calin Juravle | 249f2d3 | 2017-05-03 17:22:27 -0700 | [diff] [blame] | 1109 | return ok(); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1110 | } | 
|  | 1111 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1112 | struct stats { | 
|  | 1113 | int64_t codeSize; | 
|  | 1114 | int64_t dataSize; | 
|  | 1115 | int64_t cacheSize; | 
|  | 1116 | }; | 
|  | 1117 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1118 | #if MEASURE_DEBUG | 
|  | 1119 | static std::string toString(std::vector<int64_t> values) { | 
|  | 1120 | std::stringstream res; | 
|  | 1121 | res << "["; | 
|  | 1122 | for (size_t i = 0; i < values.size(); i++) { | 
|  | 1123 | res << values[i]; | 
|  | 1124 | if (i < values.size() - 1) { | 
|  | 1125 | res << ","; | 
|  | 1126 | } | 
|  | 1127 | } | 
|  | 1128 | res << "]"; | 
|  | 1129 | return res.str(); | 
|  | 1130 | } | 
|  | 1131 | #endif | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1132 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1133 | static void collectQuotaStats(const std::string& device, int32_t userId, | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1134 | int32_t appId, struct stats* stats, struct stats* extStats) { | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1135 | if (device.empty()) return; | 
|  | 1136 |  | 
|  | 1137 | struct dqblk dq; | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1138 |  | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1139 | if (stats != nullptr) { | 
|  | 1140 | uid_t uid = multiuser_get_uid(userId, appId); | 
|  | 1141 | if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1142 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 1143 | if (errno != ESRCH) { | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1144 | PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1145 | } | 
|  | 1146 | } else { | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1147 | #if MEASURE_DEBUG | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1148 | LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1149 | #endif | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1150 | stats->dataSize += dq.dqb_curspace; | 
|  | 1151 | } | 
|  | 1152 |  | 
|  | 1153 | int cacheGid = multiuser_get_cache_gid(userId, appId); | 
|  | 1154 | if (cacheGid != -1) { | 
|  | 1155 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid, | 
|  | 1156 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 1157 | if (errno != ESRCH) { | 
|  | 1158 | PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid; | 
|  | 1159 | } | 
|  | 1160 | } else { | 
|  | 1161 | #if MEASURE_DEBUG | 
|  | 1162 | LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace; | 
|  | 1163 | #endif | 
|  | 1164 | stats->cacheSize += dq.dqb_curspace; | 
|  | 1165 | } | 
|  | 1166 | } | 
|  | 1167 |  | 
|  | 1168 | int sharedGid = multiuser_get_shared_gid(0, appId); | 
|  | 1169 | if (sharedGid != -1) { | 
|  | 1170 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid, | 
|  | 1171 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 1172 | if (errno != ESRCH) { | 
|  | 1173 | PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid; | 
|  | 1174 | } | 
|  | 1175 | } else { | 
|  | 1176 | #if MEASURE_DEBUG | 
|  | 1177 | LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace; | 
|  | 1178 | #endif | 
|  | 1179 | stats->codeSize += dq.dqb_curspace; | 
|  | 1180 | } | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1181 | } | 
|  | 1182 | } | 
|  | 1183 |  | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1184 | if (extStats != nullptr) { | 
|  | 1185 | int extGid = multiuser_get_ext_gid(userId, appId); | 
|  | 1186 | if (extGid != -1) { | 
|  | 1187 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), extGid, | 
|  | 1188 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 1189 | if (errno != ESRCH) { | 
|  | 1190 | PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << extGid; | 
|  | 1191 | } | 
|  | 1192 | } else { | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1193 | #if MEASURE_DEBUG | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1194 | LOG(DEBUG) << "quotactl() for GID " << extGid << " " << dq.dqb_curspace; | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1195 | #endif | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1196 | extStats->dataSize += dq.dqb_curspace; | 
|  | 1197 | } | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1198 | } | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1199 |  | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1200 | int extCacheGid = multiuser_get_ext_cache_gid(userId, appId); | 
|  | 1201 | if (extCacheGid != -1) { | 
|  | 1202 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), extCacheGid, | 
|  | 1203 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 1204 | if (errno != ESRCH) { | 
|  | 1205 | PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << extCacheGid; | 
|  | 1206 | } | 
|  | 1207 | } else { | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1208 | #if MEASURE_DEBUG | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1209 | LOG(DEBUG) << "quotactl() for GID " << extCacheGid << " " << dq.dqb_curspace; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1210 | #endif | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1211 | extStats->dataSize += dq.dqb_curspace; | 
|  | 1212 | extStats->cacheSize += dq.dqb_curspace; | 
|  | 1213 | } | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1214 | } | 
|  | 1215 | } | 
|  | 1216 | } | 
|  | 1217 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1218 | static void collectManualStats(const std::string& path, struct stats* stats) { | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1219 | DIR *d; | 
|  | 1220 | int dfd; | 
|  | 1221 | struct dirent *de; | 
|  | 1222 | struct stat s; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1223 |  | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1224 | d = opendir(path.c_str()); | 
|  | 1225 | if (d == nullptr) { | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1226 | if (errno != ENOENT) { | 
|  | 1227 | PLOG(WARNING) << "Failed to open " << path; | 
|  | 1228 | } | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1229 | return; | 
|  | 1230 | } | 
|  | 1231 | dfd = dirfd(d); | 
|  | 1232 | while ((de = readdir(d))) { | 
|  | 1233 | const char *name = de->d_name; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1234 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1235 | int64_t size = 0; | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1236 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1237 | size = s.st_blocks * 512; | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1238 | } | 
|  | 1239 |  | 
|  | 1240 | if (de->d_type == DT_DIR) { | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1241 | if (!strcmp(name, ".")) { | 
|  | 1242 | // Don't recurse, but still count node size | 
|  | 1243 | } else if (!strcmp(name, "..")) { | 
|  | 1244 | // Don't recurse or count node size | 
|  | 1245 | continue; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1246 | } else { | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1247 | // Measure all children nodes | 
|  | 1248 | size = 0; | 
|  | 1249 | calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1250 | } | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1251 |  | 
|  | 1252 | if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { | 
|  | 1253 | stats->cacheSize += size; | 
|  | 1254 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1255 | } | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1256 |  | 
|  | 1257 | // Legacy symlink isn't owned by app | 
|  | 1258 | if (de->d_type == DT_LNK && !strcmp(name, "lib")) { | 
|  | 1259 | continue; | 
|  | 1260 | } | 
|  | 1261 |  | 
|  | 1262 | // Everything found inside is considered data | 
|  | 1263 | stats->dataSize += size; | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1264 | } | 
|  | 1265 | closedir(d); | 
|  | 1266 | } | 
|  | 1267 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1268 | static void collectManualStatsForUser(const std::string& path, struct stats* stats, | 
|  | 1269 | bool exclude_apps = false) { | 
|  | 1270 | DIR *d; | 
|  | 1271 | int dfd; | 
|  | 1272 | struct dirent *de; | 
|  | 1273 | struct stat s; | 
|  | 1274 |  | 
|  | 1275 | d = opendir(path.c_str()); | 
|  | 1276 | if (d == nullptr) { | 
|  | 1277 | if (errno != ENOENT) { | 
|  | 1278 | PLOG(WARNING) << "Failed to open " << path; | 
|  | 1279 | } | 
|  | 1280 | return; | 
|  | 1281 | } | 
|  | 1282 | dfd = dirfd(d); | 
|  | 1283 | while ((de = readdir(d))) { | 
|  | 1284 | if (de->d_type == DT_DIR) { | 
|  | 1285 | const char *name = de->d_name; | 
|  | 1286 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { | 
|  | 1287 | continue; | 
|  | 1288 | } | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1289 | int32_t user_uid = multiuser_get_app_id(s.st_uid); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1290 | if (!strcmp(name, ".") || !strcmp(name, "..")) { | 
|  | 1291 | continue; | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1292 | } else if (exclude_apps && (user_uid >= AID_APP_START && user_uid <= AID_APP_END)) { | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1293 | continue; | 
|  | 1294 | } else { | 
|  | 1295 | collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); | 
|  | 1296 | } | 
|  | 1297 | } | 
|  | 1298 | } | 
|  | 1299 | closedir(d); | 
|  | 1300 | } | 
|  | 1301 |  | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1302 | static void collectManualExternalStatsForUser(const std::string& path, struct stats* stats) { | 
|  | 1303 | FTS *fts; | 
|  | 1304 | FTSENT *p; | 
|  | 1305 | char *argv[] = { (char*) path.c_str(), nullptr }; | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1306 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1307 | PLOG(ERROR) << "Failed to fts_open " << path; | 
|  | 1308 | return; | 
|  | 1309 | } | 
|  | 1310 | while ((p = fts_read(fts)) != NULL) { | 
| Jeff Sharkey | 76ddaeb | 2017-01-25 22:15:42 -0700 | [diff] [blame] | 1311 | p->fts_number = p->fts_parent->fts_number; | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1312 | switch (p->fts_info) { | 
|  | 1313 | case FTS_D: | 
|  | 1314 | if (p->fts_level == 4 | 
|  | 1315 | && !strcmp(p->fts_name, "cache") | 
|  | 1316 | && !strcmp(p->fts_parent->fts_parent->fts_name, "data") | 
|  | 1317 | && !strcmp(p->fts_parent->fts_parent->fts_parent->fts_name, "Android")) { | 
|  | 1318 | p->fts_number = 1; | 
|  | 1319 | } | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1320 | // Fall through to count the directory | 
|  | 1321 | case FTS_DEFAULT: | 
|  | 1322 | case FTS_F: | 
|  | 1323 | case FTS_SL: | 
|  | 1324 | case FTS_SLNONE: | 
|  | 1325 | int64_t size = (p->fts_statp->st_blocks * 512); | 
|  | 1326 | if (p->fts_number == 1) { | 
|  | 1327 | stats->cacheSize += size; | 
|  | 1328 | } | 
|  | 1329 | stats->dataSize += size; | 
|  | 1330 | break; | 
|  | 1331 | } | 
|  | 1332 | } | 
|  | 1333 | fts_close(fts); | 
|  | 1334 | } | 
|  | 1335 |  | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1336 | binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1337 | const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, | 
|  | 1338 | int32_t appId, const std::vector<int64_t>& ceDataInodes, | 
|  | 1339 | const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1340 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1341 | CHECK_ARGUMENT_UUID(uuid); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1342 | for (auto packageName : packageNames) { | 
|  | 1343 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
|  | 1344 | } | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1345 | // NOTE: Locking is relaxed on this method, since it's limited to | 
|  | 1346 | // read-only measurements without mutation. | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1347 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1348 | // When modifying this logic, always verify using tests: | 
|  | 1349 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize | 
|  | 1350 |  | 
|  | 1351 | #if MEASURE_DEBUG | 
|  | 1352 | LOG(INFO) << "Measuring user " << userId << " app " << appId; | 
|  | 1353 | #endif | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1354 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1355 | // Here's a summary of the common storage locations across the platform, | 
|  | 1356 | // and how they're each tagged: | 
|  | 1357 | // | 
|  | 1358 | // /data/app/com.example                           UID system | 
|  | 1359 | // /data/app/com.example/oat                       UID system | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1360 | // /data/user/0/com.example                        UID u0_a10      GID u0_a10 | 
|  | 1361 | // /data/user/0/com.example/cache                  UID u0_a10      GID u0_a10_cache | 
|  | 1362 | // /data/media/0/foo.txt                           UID u0_media_rw | 
|  | 1363 | // /data/media/0/bar.jpg                           UID u0_media_rw GID u0_media_image | 
|  | 1364 | // /data/media/0/Android/data/com.example          UID u0_media_rw GID u0_a10_ext | 
|  | 1365 | // /data/media/0/Android/data/com.example/cache    UID u0_media_rw GID u0_a10_ext_cache | 
|  | 1366 | // /data/media/obb/com.example                     UID system | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1367 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1368 | struct stats stats; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1369 | struct stats extStats; | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1370 | memset(&stats, 0, sizeof(stats)); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1371 | memset(&extStats, 0, sizeof(extStats)); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1372 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1373 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1374 |  | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 1375 | auto device = findQuotaDeviceForUuid(uuid); | 
|  | 1376 | if (device.empty()) { | 
|  | 1377 | flags &= ~FLAG_USE_QUOTA; | 
|  | 1378 | } | 
|  | 1379 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1380 | ATRACE_BEGIN("obb"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1381 | for (auto packageName : packageNames) { | 
|  | 1382 | auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str()); | 
|  | 1383 | calculate_tree_size(obbCodePath, &extStats.codeSize); | 
|  | 1384 | } | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1385 | ATRACE_END(); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1386 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1387 | if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) { | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1388 | ATRACE_BEGIN("code"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1389 | for (auto codePath : codePaths) { | 
|  | 1390 | calculate_tree_size(codePath, &stats.codeSize, -1, | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1391 | multiuser_get_shared_gid(0, appId)); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1392 | } | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1393 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1394 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1395 | ATRACE_BEGIN("quota"); | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 1396 | collectQuotaStats(device, userId, appId, &stats, &extStats); | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1397 | ATRACE_END(); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1398 | } else { | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1399 | ATRACE_BEGIN("code"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1400 | for (auto codePath : codePaths) { | 
|  | 1401 | calculate_tree_size(codePath, &stats.codeSize); | 
|  | 1402 | } | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1403 | ATRACE_END(); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1404 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1405 | for (size_t i = 0; i < packageNames.size(); i++) { | 
|  | 1406 | const char* pkgname = packageNames[i].c_str(); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1407 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1408 | ATRACE_BEGIN("data"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1409 | auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); | 
|  | 1410 | collectManualStats(cePath, &stats); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1411 | auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); | 
|  | 1412 | collectManualStats(dePath, &stats); | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1413 | ATRACE_END(); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1414 |  | 
| Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1415 | if (!uuid) { | 
|  | 1416 | ATRACE_BEGIN("profiles"); | 
|  | 1417 | calculate_tree_size( | 
|  | 1418 | create_primary_current_profile_package_dir_path(userId, pkgname), | 
|  | 1419 | &stats.dataSize); | 
|  | 1420 | calculate_tree_size( | 
|  | 1421 | create_primary_reference_profile_package_dir_path(pkgname), | 
|  | 1422 | &stats.codeSize); | 
|  | 1423 | ATRACE_END(); | 
|  | 1424 | } | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1425 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1426 | ATRACE_BEGIN("external"); | 
| Jeff Sharkey | 76ddaeb | 2017-01-25 22:15:42 -0700 | [diff] [blame] | 1427 | auto extPath = create_data_media_package_path(uuid_, userId, "data", pkgname); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1428 | collectManualStats(extPath, &extStats); | 
| Jeff Sharkey | 76ddaeb | 2017-01-25 22:15:42 -0700 | [diff] [blame] | 1429 | auto mediaPath = create_data_media_package_path(uuid_, userId, "media", pkgname); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1430 | calculate_tree_size(mediaPath, &extStats.dataSize); | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1431 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1432 | } | 
|  | 1433 |  | 
| Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1434 | if (!uuid) { | 
|  | 1435 | ATRACE_BEGIN("dalvik"); | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1436 | int32_t sharedGid = multiuser_get_shared_gid(0, appId); | 
| Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1437 | if (sharedGid != -1) { | 
|  | 1438 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, | 
|  | 1439 | sharedGid, -1); | 
|  | 1440 | } | 
|  | 1441 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1442 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1443 | } | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1444 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1445 | std::vector<int64_t> ret; | 
|  | 1446 | ret.push_back(stats.codeSize); | 
|  | 1447 | ret.push_back(stats.dataSize); | 
|  | 1448 | ret.push_back(stats.cacheSize); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1449 | ret.push_back(extStats.codeSize); | 
|  | 1450 | ret.push_back(extStats.dataSize); | 
|  | 1451 | ret.push_back(extStats.cacheSize); | 
|  | 1452 | #if MEASURE_DEBUG | 
|  | 1453 | LOG(DEBUG) << "Final result " << toString(ret); | 
|  | 1454 | #endif | 
|  | 1455 | *_aidl_return = ret; | 
|  | 1456 | return ok(); | 
|  | 1457 | } | 
|  | 1458 |  | 
|  | 1459 | binder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid, | 
|  | 1460 | int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, | 
|  | 1461 | std::vector<int64_t>* _aidl_return) { | 
|  | 1462 | ENFORCE_UID(AID_SYSTEM); | 
|  | 1463 | CHECK_ARGUMENT_UUID(uuid); | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1464 | // NOTE: Locking is relaxed on this method, since it's limited to | 
|  | 1465 | // read-only measurements without mutation. | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1466 |  | 
|  | 1467 | // When modifying this logic, always verify using tests: | 
|  | 1468 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize | 
|  | 1469 |  | 
|  | 1470 | #if MEASURE_DEBUG | 
|  | 1471 | LOG(INFO) << "Measuring user " << userId; | 
|  | 1472 | #endif | 
|  | 1473 |  | 
|  | 1474 | struct stats stats; | 
|  | 1475 | struct stats extStats; | 
|  | 1476 | memset(&stats, 0, sizeof(stats)); | 
|  | 1477 | memset(&extStats, 0, sizeof(extStats)); | 
|  | 1478 |  | 
|  | 1479 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 1480 |  | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 1481 | auto device = findQuotaDeviceForUuid(uuid); | 
|  | 1482 | if (device.empty()) { | 
|  | 1483 | flags &= ~FLAG_USE_QUOTA; | 
|  | 1484 | } | 
|  | 1485 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1486 | if (flags & FLAG_USE_QUOTA) { | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1487 | struct dqblk dq; | 
|  | 1488 |  | 
|  | 1489 | ATRACE_BEGIN("obb"); | 
|  | 1490 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), AID_MEDIA_OBB, | 
|  | 1491 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 1492 | if (errno != ESRCH) { | 
|  | 1493 | PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << AID_MEDIA_OBB; | 
|  | 1494 | } | 
|  | 1495 | } else { | 
|  | 1496 | #if MEASURE_DEBUG | 
|  | 1497 | LOG(DEBUG) << "quotactl() for GID " << AID_MEDIA_OBB << " " << dq.dqb_curspace; | 
|  | 1498 | #endif | 
|  | 1499 | extStats.codeSize += dq.dqb_curspace; | 
|  | 1500 | } | 
|  | 1501 | ATRACE_END(); | 
|  | 1502 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1503 | ATRACE_BEGIN("code"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1504 | calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1505 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1506 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1507 | ATRACE_BEGIN("data"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1508 | auto cePath = create_data_user_ce_path(uuid_, userId); | 
|  | 1509 | collectManualStatsForUser(cePath, &stats, true); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1510 | auto dePath = create_data_user_de_path(uuid_, userId); | 
|  | 1511 | collectManualStatsForUser(dePath, &stats, true); | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1512 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1513 |  | 
| Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1514 | if (!uuid) { | 
|  | 1515 | ATRACE_BEGIN("profile"); | 
|  | 1516 | auto userProfilePath = create_primary_cur_profile_dir_path(userId); | 
|  | 1517 | calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); | 
|  | 1518 | auto refProfilePath = create_primary_ref_profile_dir_path(); | 
|  | 1519 | calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); | 
|  | 1520 | ATRACE_END(); | 
|  | 1521 | } | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1522 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1523 | ATRACE_BEGIN("external"); | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1524 | uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); | 
|  | 1525 | if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, | 
|  | 1526 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 1527 | if (errno != ESRCH) { | 
|  | 1528 | PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; | 
|  | 1529 | } | 
|  | 1530 | } else { | 
|  | 1531 | #if MEASURE_DEBUG | 
|  | 1532 | LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1533 | #endif | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1534 | extStats.dataSize += dq.dqb_curspace; | 
|  | 1535 | } | 
|  | 1536 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1537 |  | 
| Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1538 | if (!uuid) { | 
|  | 1539 | ATRACE_BEGIN("dalvik"); | 
|  | 1540 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, | 
|  | 1541 | -1, -1, true); | 
|  | 1542 | calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize, | 
|  | 1543 | -1, -1, true); | 
|  | 1544 | ATRACE_END(); | 
|  | 1545 | } | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1546 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1547 | ATRACE_BEGIN("quota"); | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1548 | int64_t dataSize = extStats.dataSize; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1549 | for (auto appId : appIds) { | 
|  | 1550 | if (appId >= AID_APP_START) { | 
|  | 1551 | collectQuotaStats(device, userId, appId, &stats, &extStats); | 
| Jeff Sharkey | ec1be62 | 2017-04-10 00:19:46 -0600 | [diff] [blame] | 1552 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1553 | #if MEASURE_DEBUG | 
|  | 1554 | // Sleep to make sure we don't lose logs | 
|  | 1555 | usleep(1); | 
|  | 1556 | #endif | 
|  | 1557 | } | 
|  | 1558 | } | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1559 | extStats.dataSize = dataSize; | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1560 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1561 | } else { | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1562 | ATRACE_BEGIN("obb"); | 
|  | 1563 | auto obbPath = create_data_path(uuid_) + "/media/obb"; | 
|  | 1564 | calculate_tree_size(obbPath, &extStats.codeSize); | 
|  | 1565 | ATRACE_END(); | 
|  | 1566 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1567 | ATRACE_BEGIN("code"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1568 | calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1569 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1570 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1571 | ATRACE_BEGIN("data"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1572 | auto cePath = create_data_user_ce_path(uuid_, userId); | 
|  | 1573 | collectManualStatsForUser(cePath, &stats); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1574 | auto dePath = create_data_user_de_path(uuid_, userId); | 
|  | 1575 | collectManualStatsForUser(dePath, &stats); | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1576 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1577 |  | 
| Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1578 | if (!uuid) { | 
|  | 1579 | ATRACE_BEGIN("profile"); | 
|  | 1580 | auto userProfilePath = create_primary_cur_profile_dir_path(userId); | 
|  | 1581 | calculate_tree_size(userProfilePath, &stats.dataSize); | 
|  | 1582 | auto refProfilePath = create_primary_ref_profile_dir_path(); | 
|  | 1583 | calculate_tree_size(refProfilePath, &stats.codeSize); | 
|  | 1584 | ATRACE_END(); | 
|  | 1585 | } | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1586 |  | 
| Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1587 | ATRACE_BEGIN("external"); | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1588 | auto dataMediaPath = create_data_media_path(uuid_, userId); | 
|  | 1589 | collectManualExternalStatsForUser(dataMediaPath, &extStats); | 
|  | 1590 | #if MEASURE_DEBUG | 
|  | 1591 | LOG(DEBUG) << "Measured external data " << extStats.dataSize << " cache " | 
|  | 1592 | << extStats.cacheSize; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1593 | #endif | 
| Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1594 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1595 |  | 
| Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1596 | if (!uuid) { | 
|  | 1597 | ATRACE_BEGIN("dalvik"); | 
|  | 1598 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); | 
|  | 1599 | calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize); | 
|  | 1600 | ATRACE_END(); | 
|  | 1601 | } | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1602 | } | 
|  | 1603 |  | 
|  | 1604 | std::vector<int64_t> ret; | 
|  | 1605 | ret.push_back(stats.codeSize); | 
|  | 1606 | ret.push_back(stats.dataSize); | 
|  | 1607 | ret.push_back(stats.cacheSize); | 
|  | 1608 | ret.push_back(extStats.codeSize); | 
|  | 1609 | ret.push_back(extStats.dataSize); | 
|  | 1610 | ret.push_back(extStats.cacheSize); | 
|  | 1611 | #if MEASURE_DEBUG | 
|  | 1612 | LOG(DEBUG) << "Final result " << toString(ret); | 
|  | 1613 | #endif | 
|  | 1614 | *_aidl_return = ret; | 
|  | 1615 | return ok(); | 
|  | 1616 | } | 
|  | 1617 |  | 
|  | 1618 | binder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid, | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1619 | int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, | 
|  | 1620 | std::vector<int64_t>* _aidl_return) { | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1621 | ENFORCE_UID(AID_SYSTEM); | 
|  | 1622 | CHECK_ARGUMENT_UUID(uuid); | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1623 | // NOTE: Locking is relaxed on this method, since it's limited to | 
|  | 1624 | // read-only measurements without mutation. | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1625 |  | 
|  | 1626 | // When modifying this logic, always verify using tests: | 
|  | 1627 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize | 
|  | 1628 |  | 
|  | 1629 | #if MEASURE_DEBUG | 
|  | 1630 | LOG(INFO) << "Measuring external " << userId; | 
|  | 1631 | #endif | 
|  | 1632 |  | 
|  | 1633 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 1634 |  | 
|  | 1635 | int64_t totalSize = 0; | 
|  | 1636 | int64_t audioSize = 0; | 
|  | 1637 | int64_t videoSize = 0; | 
|  | 1638 | int64_t imageSize = 0; | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1639 | int64_t appSize = 0; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1640 |  | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 1641 | auto device = findQuotaDeviceForUuid(uuid); | 
|  | 1642 | if (device.empty()) { | 
|  | 1643 | flags &= ~FLAG_USE_QUOTA; | 
|  | 1644 | } | 
|  | 1645 |  | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1646 | if (flags & FLAG_USE_QUOTA) { | 
|  | 1647 | struct dqblk dq; | 
|  | 1648 |  | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1649 | ATRACE_BEGIN("quota"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1650 | uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); | 
|  | 1651 | if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, | 
|  | 1652 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 1653 | if (errno != ESRCH) { | 
|  | 1654 | PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; | 
|  | 1655 | } | 
|  | 1656 | } else { | 
|  | 1657 | #if MEASURE_DEBUG | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1658 | LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1659 | #endif | 
|  | 1660 | totalSize = dq.dqb_curspace; | 
|  | 1661 | } | 
|  | 1662 |  | 
|  | 1663 | gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); | 
|  | 1664 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid, | 
|  | 1665 | reinterpret_cast<char*>(&dq)) == 0) { | 
|  | 1666 | #if MEASURE_DEBUG | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1667 | LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1668 | #endif | 
|  | 1669 | audioSize = dq.dqb_curspace; | 
|  | 1670 | } | 
|  | 1671 | gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); | 
|  | 1672 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid, | 
|  | 1673 | reinterpret_cast<char*>(&dq)) == 0) { | 
|  | 1674 | #if MEASURE_DEBUG | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1675 | LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1676 | #endif | 
|  | 1677 | videoSize = dq.dqb_curspace; | 
|  | 1678 | } | 
|  | 1679 | gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); | 
|  | 1680 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid, | 
|  | 1681 | reinterpret_cast<char*>(&dq)) == 0) { | 
|  | 1682 | #if MEASURE_DEBUG | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1683 | LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1684 | #endif | 
|  | 1685 | imageSize = dq.dqb_curspace; | 
|  | 1686 | } | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1687 | ATRACE_END(); | 
|  | 1688 |  | 
|  | 1689 | ATRACE_BEGIN("apps"); | 
|  | 1690 | struct stats extStats; | 
|  | 1691 | memset(&extStats, 0, sizeof(extStats)); | 
|  | 1692 | for (auto appId : appIds) { | 
|  | 1693 | if (appId >= AID_APP_START) { | 
|  | 1694 | collectQuotaStats(device, userId, appId, nullptr, &extStats); | 
|  | 1695 | } | 
|  | 1696 | } | 
|  | 1697 | appSize = extStats.dataSize + extStats.cacheSize; | 
|  | 1698 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1699 | } else { | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1700 | ATRACE_BEGIN("manual"); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1701 | FTS *fts; | 
|  | 1702 | FTSENT *p; | 
|  | 1703 | auto path = create_data_media_path(uuid_, userId); | 
|  | 1704 | char *argv[] = { (char*) path.c_str(), nullptr }; | 
| Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1705 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1706 | return error("Failed to fts_open " + path); | 
|  | 1707 | } | 
|  | 1708 | while ((p = fts_read(fts)) != NULL) { | 
|  | 1709 | char* ext; | 
|  | 1710 | int64_t size = (p->fts_statp->st_blocks * 512); | 
|  | 1711 | switch (p->fts_info) { | 
|  | 1712 | case FTS_F: | 
|  | 1713 | // Only categorize files not belonging to apps | 
| Jeff Sharkey | 444ad1e | 2017-03-12 16:25:36 -0600 | [diff] [blame] | 1714 | if (p->fts_parent->fts_number == 0) { | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1715 | ext = strrchr(p->fts_name, '.'); | 
|  | 1716 | if (ext != nullptr) { | 
|  | 1717 | switch (MatchExtension(++ext)) { | 
|  | 1718 | case AID_MEDIA_AUDIO: audioSize += size; break; | 
|  | 1719 | case AID_MEDIA_VIDEO: videoSize += size; break; | 
|  | 1720 | case AID_MEDIA_IMAGE: imageSize += size; break; | 
|  | 1721 | } | 
|  | 1722 | } | 
|  | 1723 | } | 
|  | 1724 | // Fall through to always count against total | 
|  | 1725 | case FTS_D: | 
| Jeff Sharkey | 444ad1e | 2017-03-12 16:25:36 -0600 | [diff] [blame] | 1726 | // Ignore data belonging to specific apps | 
|  | 1727 | p->fts_number = p->fts_parent->fts_number; | 
|  | 1728 | if (p->fts_level == 1 && !strcmp(p->fts_name, "Android")) { | 
|  | 1729 | p->fts_number = 1; | 
|  | 1730 | } | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1731 | case FTS_DEFAULT: | 
|  | 1732 | case FTS_SL: | 
|  | 1733 | case FTS_SLNONE: | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1734 | if (p->fts_parent->fts_number == 1) { | 
|  | 1735 | appSize += size; | 
|  | 1736 | } | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1737 | totalSize += size; | 
|  | 1738 | break; | 
|  | 1739 | } | 
|  | 1740 | } | 
|  | 1741 | fts_close(fts); | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1742 | ATRACE_END(); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1743 | } | 
|  | 1744 |  | 
|  | 1745 | std::vector<int64_t> ret; | 
|  | 1746 | ret.push_back(totalSize); | 
|  | 1747 | ret.push_back(audioSize); | 
|  | 1748 | ret.push_back(videoSize); | 
|  | 1749 | ret.push_back(imageSize); | 
| Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1750 | ret.push_back(appSize); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1751 | #if MEASURE_DEBUG | 
|  | 1752 | LOG(DEBUG) << "Final result " << toString(ret); | 
|  | 1753 | #endif | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1754 | *_aidl_return = ret; | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1755 | return ok(); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1756 | } | 
|  | 1757 |  | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1758 | binder::Status InstalldNativeService::setAppQuota(const std::unique_ptr<std::string>& uuid, | 
|  | 1759 | int32_t userId, int32_t appId, int64_t cacheQuota) { | 
|  | 1760 | ENFORCE_UID(AID_SYSTEM); | 
|  | 1761 | CHECK_ARGUMENT_UUID(uuid); | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 1762 | std::lock_guard<std::recursive_mutex> lock(mQuotasLock); | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1763 |  | 
|  | 1764 | int32_t uid = multiuser_get_uid(userId, appId); | 
|  | 1765 | mCacheQuotas[uid] = cacheQuota; | 
|  | 1766 |  | 
|  | 1767 | return ok(); | 
|  | 1768 | } | 
|  | 1769 |  | 
| David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 1770 | // Dumps the contents of a profile file, using pkgname's dex files for pretty | 
|  | 1771 | // printing the result. | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1772 | binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, | 
|  | 1773 | const std::string& codePaths, bool* _aidl_return) { | 
|  | 1774 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1775 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1776 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1777 |  | 
|  | 1778 | const char* pkgname = packageName.c_str(); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1779 | const char* code_paths = codePaths.c_str(); | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1780 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1781 | *_aidl_return = dump_profiles(uid, pkgname, code_paths); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1782 | return ok(); | 
| David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 1783 | } | 
|  | 1784 |  | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 1785 | // Copy the contents of a system profile over the data profile. | 
|  | 1786 | binder::Status InstalldNativeService::copySystemProfile(const std::string& systemProfile, | 
|  | 1787 | int32_t packageUid, const std::string& packageName, bool* _aidl_return) { | 
|  | 1788 | ENFORCE_UID(AID_SYSTEM); | 
|  | 1789 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
|  | 1790 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
|  | 1791 | *_aidl_return = copy_system_profile(systemProfile, packageUid, packageName); | 
|  | 1792 | return ok(); | 
|  | 1793 | } | 
|  | 1794 |  | 
| Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 1795 | // TODO: Consider returning error codes. | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1796 | binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1797 | bool* _aidl_return) { | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1798 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1799 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1800 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1801 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1802 | *_aidl_return = analyze_primary_profiles(uid, packageName); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1803 | return ok(); | 
| Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 1804 | } | 
|  | 1805 |  | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1806 | binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, | 
|  | 1807 | const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, | 
|  | 1808 | int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, | 
|  | 1809 | const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, | 
| Calin Juravle | cb556e3 | 2017-04-04 20:22:50 -0700 | [diff] [blame] | 1810 | const std::unique_ptr<std::string>& sharedLibraries, | 
|  | 1811 | const std::unique_ptr<std::string>& seInfo) { | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1812 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1813 | CHECK_ARGUMENT_UUID(uuid); | 
|  | 1814 | if (packageName && *packageName != "*") { | 
|  | 1815 | CHECK_ARGUMENT_PACKAGE_NAME(*packageName); | 
|  | 1816 | } | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1817 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1818 |  | 
|  | 1819 | const char* apk_path = apkPath.c_str(); | 
|  | 1820 | const char* pkgname = packageName ? packageName->c_str() : "*"; | 
|  | 1821 | const char* instruction_set = instructionSet.c_str(); | 
|  | 1822 | const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; | 
|  | 1823 | const char* compiler_filter = compilerFilter.c_str(); | 
|  | 1824 | const char* volume_uuid = uuid ? uuid->c_str() : nullptr; | 
|  | 1825 | const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; | 
| Calin Juravle | cb556e3 | 2017-04-04 20:22:50 -0700 | [diff] [blame] | 1826 | const char* se_info = seInfo ? seInfo->c_str() : nullptr; | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1827 | int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, | 
| Calin Juravle | cb556e3 | 2017-04-04 20:22:50 -0700 | [diff] [blame] | 1828 | oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries, se_info); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1829 | return res ? error(res, "Failed to dexopt") : ok(); | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1830 | } | 
|  | 1831 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1832 | binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { | 
|  | 1833 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1834 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
|  | 1835 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1836 | const char* instruction_set = instructionSet.c_str(); | 
|  | 1837 |  | 
|  | 1838 | char boot_marker_path[PKG_PATH_MAX]; | 
|  | 1839 | sprintf(boot_marker_path, | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 1840 | "%s/%s/%s/.booting", | 
|  | 1841 | android_data_dir.path, | 
|  | 1842 | DALVIK_CACHE, | 
|  | 1843 | instruction_set); | 
| Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 1844 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1845 | ALOGV("mark_boot_complete : %s", boot_marker_path); | 
|  | 1846 | if (unlink(boot_marker_path) != 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1847 | return error(StringPrintf("Failed to unlink %s", boot_marker_path)); | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1848 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1849 | return ok(); | 
| Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 1850 | } | 
|  | 1851 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1852 | void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, | 
|  | 1853 | struct stat* statbuf) | 
|  | 1854 | { | 
|  | 1855 | while (path[basepos] != 0) { | 
|  | 1856 | if (path[basepos] == '/') { | 
|  | 1857 | path[basepos] = 0; | 
|  | 1858 | if (lstat(path, statbuf) < 0) { | 
|  | 1859 | ALOGV("Making directory: %s\n", path); | 
|  | 1860 | if (mkdir(path, mode) == 0) { | 
|  | 1861 | chown(path, uid, gid); | 
|  | 1862 | } else { | 
|  | 1863 | ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); | 
|  | 1864 | } | 
|  | 1865 | } | 
|  | 1866 | path[basepos] = '/'; | 
|  | 1867 | basepos++; | 
|  | 1868 | } | 
|  | 1869 | basepos++; | 
|  | 1870 | } | 
|  | 1871 | } | 
|  | 1872 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1873 | binder::Status InstalldNativeService::linkNativeLibraryDirectory( | 
|  | 1874 | const std::unique_ptr<std::string>& uuid, const std::string& packageName, | 
|  | 1875 | const std::string& nativeLibPath32, int32_t userId) { | 
|  | 1876 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1877 | CHECK_ARGUMENT_UUID(uuid); | 
|  | 1878 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1879 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1880 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1881 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 1882 | const char* pkgname = packageName.c_str(); | 
|  | 1883 | const char* asecLibDir = nativeLibPath32.c_str(); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1884 | struct stat s, libStat; | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1885 | binder::Status res = ok(); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1886 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1887 | auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); | 
|  | 1888 | auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; | 
| Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 1889 |  | 
|  | 1890 | const char* pkgdir = _pkgdir.c_str(); | 
|  | 1891 | const char* libsymlink = _libsymlink.c_str(); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1892 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1893 | if (stat(pkgdir, &s) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1894 | return error("Failed to stat " + _pkgdir); | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1895 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1896 |  | 
|  | 1897 | if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1898 | return error("Failed to chown " + _pkgdir); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1899 | } | 
|  | 1900 |  | 
|  | 1901 | if (chmod(pkgdir, 0700) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1902 | res = error("Failed to chmod " + _pkgdir); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1903 | goto out; | 
|  | 1904 | } | 
|  | 1905 |  | 
|  | 1906 | if (lstat(libsymlink, &libStat) < 0) { | 
|  | 1907 | if (errno != ENOENT) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1908 | res = error("Failed to stat " + _libsymlink); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1909 | goto out; | 
|  | 1910 | } | 
|  | 1911 | } else { | 
|  | 1912 | if (S_ISDIR(libStat.st_mode)) { | 
| Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 1913 | if (delete_dir_contents(libsymlink, 1, NULL) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1914 | res = error("Failed to delete " + _libsymlink); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1915 | goto out; | 
|  | 1916 | } | 
|  | 1917 | } else if (S_ISLNK(libStat.st_mode)) { | 
|  | 1918 | if (unlink(libsymlink) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1919 | res = error("Failed to unlink " + _libsymlink); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1920 | goto out; | 
|  | 1921 | } | 
|  | 1922 | } | 
|  | 1923 | } | 
|  | 1924 |  | 
|  | 1925 | if (symlink(asecLibDir, libsymlink) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1926 | res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1927 | goto out; | 
|  | 1928 | } | 
|  | 1929 |  | 
|  | 1930 | out: | 
|  | 1931 | if (chmod(pkgdir, s.st_mode) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1932 | auto msg = "Failed to cleanup chmod " + _pkgdir; | 
|  | 1933 | if (res.isOk()) { | 
|  | 1934 | res = error(msg); | 
|  | 1935 | } else { | 
|  | 1936 | PLOG(ERROR) << msg; | 
|  | 1937 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1938 | } | 
|  | 1939 |  | 
|  | 1940 | if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1941 | auto msg = "Failed to cleanup chown " + _pkgdir; | 
|  | 1942 | if (res.isOk()) { | 
|  | 1943 | res = error(msg); | 
|  | 1944 | } else { | 
|  | 1945 | PLOG(ERROR) << msg; | 
|  | 1946 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1947 | } | 
|  | 1948 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1949 | return res; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1950 | } | 
| MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1951 |  | 
|  | 1952 | static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) | 
|  | 1953 | { | 
|  | 1954 | static const char *IDMAP_BIN = "/system/bin/idmap"; | 
|  | 1955 | static const size_t MAX_INT_LEN = 32; | 
|  | 1956 | char idmap_str[MAX_INT_LEN]; | 
|  | 1957 |  | 
|  | 1958 | snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); | 
|  | 1959 |  | 
|  | 1960 | execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); | 
|  | 1961 | ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); | 
|  | 1962 | } | 
|  | 1963 |  | 
|  | 1964 | // Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) | 
|  | 1965 | // eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap | 
|  | 1966 | static int flatten_path(const char *prefix, const char *suffix, | 
|  | 1967 | const char *overlay_path, char *idmap_path, size_t N) | 
|  | 1968 | { | 
|  | 1969 | if (overlay_path == NULL || idmap_path == NULL) { | 
|  | 1970 | return -1; | 
|  | 1971 | } | 
|  | 1972 | const size_t len_overlay_path = strlen(overlay_path); | 
|  | 1973 | // will access overlay_path + 1 further below; requires absolute path | 
|  | 1974 | if (len_overlay_path < 2 || *overlay_path != '/') { | 
|  | 1975 | return -1; | 
|  | 1976 | } | 
|  | 1977 | const size_t len_idmap_root = strlen(prefix); | 
|  | 1978 | const size_t len_suffix = strlen(suffix); | 
|  | 1979 | if (SIZE_MAX - len_idmap_root < len_overlay_path || | 
|  | 1980 | SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { | 
|  | 1981 | // additions below would cause overflow | 
|  | 1982 | return -1; | 
|  | 1983 | } | 
|  | 1984 | if (N < len_idmap_root + len_overlay_path + len_suffix) { | 
|  | 1985 | return -1; | 
|  | 1986 | } | 
|  | 1987 | memset(idmap_path, 0, N); | 
|  | 1988 | snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); | 
|  | 1989 | char *ch = idmap_path + len_idmap_root; | 
|  | 1990 | while (*ch != '\0') { | 
|  | 1991 | if (*ch == '/') { | 
|  | 1992 | *ch = '@'; | 
|  | 1993 | } | 
|  | 1994 | ++ch; | 
|  | 1995 | } | 
|  | 1996 | return 0; | 
|  | 1997 | } | 
|  | 1998 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1999 | binder::Status InstalldNativeService::idmap(const std::string& targetApkPath, | 
|  | 2000 | const std::string& overlayApkPath, int32_t uid) { | 
|  | 2001 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2002 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
|  | 2003 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2004 | const char* target_apk = targetApkPath.c_str(); | 
|  | 2005 | const char* overlay_apk = overlayApkPath.c_str(); | 
| MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 2006 | ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); | 
|  | 2007 |  | 
|  | 2008 | int idmap_fd = -1; | 
|  | 2009 | char idmap_path[PATH_MAX]; | 
|  | 2010 |  | 
|  | 2011 | if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, | 
|  | 2012 | idmap_path, sizeof(idmap_path)) == -1) { | 
|  | 2013 | ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); | 
|  | 2014 | goto fail; | 
|  | 2015 | } | 
|  | 2016 |  | 
| Adam Lesinski | b6182b3 | 2017-05-16 22:06:42 +0000 | [diff] [blame] | 2017 | unlink(idmap_path); | 
|  | 2018 | idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); | 
| MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 2019 | if (idmap_fd < 0) { | 
|  | 2020 | ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); | 
|  | 2021 | goto fail; | 
|  | 2022 | } | 
|  | 2023 | if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { | 
|  | 2024 | ALOGE("idmap cannot chown '%s'\n", idmap_path); | 
|  | 2025 | goto fail; | 
|  | 2026 | } | 
|  | 2027 | if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { | 
|  | 2028 | ALOGE("idmap cannot chmod '%s'\n", idmap_path); | 
|  | 2029 | goto fail; | 
|  | 2030 | } | 
|  | 2031 |  | 
|  | 2032 | pid_t pid; | 
|  | 2033 | pid = fork(); | 
|  | 2034 | if (pid == 0) { | 
|  | 2035 | /* child -- drop privileges before continuing */ | 
|  | 2036 | if (setgid(uid) != 0) { | 
|  | 2037 | ALOGE("setgid(%d) failed during idmap\n", uid); | 
|  | 2038 | exit(1); | 
|  | 2039 | } | 
|  | 2040 | if (setuid(uid) != 0) { | 
|  | 2041 | ALOGE("setuid(%d) failed during idmap\n", uid); | 
|  | 2042 | exit(1); | 
|  | 2043 | } | 
|  | 2044 | if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { | 
|  | 2045 | ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); | 
|  | 2046 | exit(1); | 
|  | 2047 | } | 
|  | 2048 |  | 
|  | 2049 | run_idmap(target_apk, overlay_apk, idmap_fd); | 
|  | 2050 | exit(1); /* only if exec call to idmap failed */ | 
|  | 2051 | } else { | 
|  | 2052 | int status = wait_child(pid); | 
|  | 2053 | if (status != 0) { | 
|  | 2054 | ALOGE("idmap failed, status=0x%04x\n", status); | 
|  | 2055 | goto fail; | 
|  | 2056 | } | 
|  | 2057 | } | 
|  | 2058 |  | 
|  | 2059 | close(idmap_fd); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2060 | return ok(); | 
| MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 2061 | fail: | 
|  | 2062 | if (idmap_fd >= 0) { | 
|  | 2063 | close(idmap_fd); | 
|  | 2064 | unlink(idmap_path); | 
|  | 2065 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2066 | return error(); | 
| MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 2067 | } | 
| Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 2068 |  | 
| MÃ¥rten Kongstad | 5c6944c | 2015-12-15 14:02:30 +0100 | [diff] [blame] | 2069 | binder::Status InstalldNativeService::removeIdmap(const std::string& overlayApkPath) { | 
|  | 2070 | const char* overlay_apk = overlayApkPath.c_str(); | 
|  | 2071 | char idmap_path[PATH_MAX]; | 
|  | 2072 |  | 
|  | 2073 | if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, | 
|  | 2074 | idmap_path, sizeof(idmap_path)) == -1) { | 
|  | 2075 | ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); | 
|  | 2076 | return error(); | 
|  | 2077 | } | 
|  | 2078 | if (unlink(idmap_path) < 0) { | 
|  | 2079 | ALOGE("couldn't unlink idmap file %s\n", idmap_path); | 
|  | 2080 | return error(); | 
|  | 2081 | } | 
|  | 2082 | return ok(); | 
|  | 2083 | } | 
|  | 2084 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2085 | binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, | 
|  | 2086 | const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, | 
|  | 2087 | const std::string& seInfo) { | 
|  | 2088 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2089 | CHECK_ARGUMENT_UUID(uuid); | 
|  | 2090 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2091 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2092 |  | 
|  | 2093 | binder::Status res = ok(); | 
| Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 2094 |  | 
| Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 2095 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 2096 | unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2097 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 2098 | const char* pkgName = packageName.c_str(); | 
|  | 2099 | const char* seinfo = seInfo.c_str(); | 
| Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 2100 |  | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2101 | uid_t uid = multiuser_get_uid(userId, appId); | 
| Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 2102 | if (flags & FLAG_STORAGE_CE) { | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2103 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 2104 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2105 | res = error("restorecon failed for " + path); | 
| Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 2106 | } | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 2107 | } | 
| Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 2108 | if (flags & FLAG_STORAGE_DE) { | 
| Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2109 | auto path = create_data_user_de_package_path(uuid_, userId, pkgName); | 
| Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 2110 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2111 | res = error("restorecon failed for " + path); | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 2112 | } | 
| Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 2113 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2114 | return res; | 
| Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 2115 | } | 
| Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 2116 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2117 | binder::Status InstalldNativeService::createOatDir(const std::string& oatDir, | 
|  | 2118 | const std::string& instructionSet) { | 
|  | 2119 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2120 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
|  | 2121 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2122 | const char* oat_dir = oatDir.c_str(); | 
|  | 2123 | const char* instruction_set = instructionSet.c_str(); | 
| Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2124 | char oat_instr_dir[PKG_PATH_MAX]; | 
|  | 2125 |  | 
|  | 2126 | if (validate_apk_path(oat_dir)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2127 | return error("Invalid path " + oatDir); | 
| Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2128 | } | 
| Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 2129 | if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2130 | return error("Failed to prepare " + oatDir); | 
| Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2131 | } | 
|  | 2132 | if (selinux_android_restorecon(oat_dir, 0)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2133 | return error("Failed to restorecon " + oatDir); | 
| Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2134 | } | 
|  | 2135 | snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); | 
| Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 2136 | if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2137 | return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); | 
| Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2138 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2139 | return ok(); | 
| Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2140 | } | 
|  | 2141 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2142 | binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { | 
|  | 2143 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2144 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
|  | 2145 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2146 | if (validate_apk_path(packageDir.c_str())) { | 
|  | 2147 | return error("Invalid path " + packageDir); | 
| Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2148 | } | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2149 | if (delete_dir_contents_and_dir(packageDir) != 0) { | 
|  | 2150 | return error("Failed to delete " + packageDir); | 
|  | 2151 | } | 
|  | 2152 | return ok(); | 
| Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2153 | } | 
|  | 2154 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2155 | binder::Status InstalldNativeService::linkFile(const std::string& relativePath, | 
|  | 2156 | const std::string& fromBase, const std::string& toBase) { | 
|  | 2157 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2158 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
|  | 2159 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2160 | const char* relative_path = relativePath.c_str(); | 
|  | 2161 | const char* from_base = fromBase.c_str(); | 
|  | 2162 | const char* to_base = toBase.c_str(); | 
| Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 2163 | char from_path[PKG_PATH_MAX]; | 
|  | 2164 | char to_path[PKG_PATH_MAX]; | 
|  | 2165 | snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); | 
|  | 2166 | snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); | 
|  | 2167 |  | 
|  | 2168 | if (validate_apk_path_subdirs(from_path)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2169 | return error(StringPrintf("Invalid from path %s", from_path)); | 
| Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 2170 | } | 
|  | 2171 |  | 
|  | 2172 | if (validate_apk_path_subdirs(to_path)) { | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2173 | return error(StringPrintf("Invalid to path %s", to_path)); | 
| Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 2174 | } | 
|  | 2175 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2176 | if (link(from_path, to_path) < 0) { | 
|  | 2177 | return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); | 
| Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 2178 | } | 
|  | 2179 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2180 | return ok(); | 
| Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 2181 | } | 
|  | 2182 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2183 | binder::Status InstalldNativeService::moveAb(const std::string& apkPath, | 
|  | 2184 | const std::string& instructionSet, const std::string& outputPath) { | 
|  | 2185 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2186 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2187 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2188 | const char* apk_path = apkPath.c_str(); | 
|  | 2189 | const char* instruction_set = instructionSet.c_str(); | 
|  | 2190 | const char* oat_dir = outputPath.c_str(); | 
| Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 2191 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2192 | bool success = move_ab(apk_path, instruction_set, oat_dir); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2193 | return success ? ok() : error(); | 
| Andreas Gampe | e65b4fa | 2016-03-01 11:46:45 -0800 | [diff] [blame] | 2194 | } | 
|  | 2195 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2196 | binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, | 
|  | 2197 | const std::string& instructionSet, const std::string& outputPath) { | 
|  | 2198 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2199 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2200 |  | 
| Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2201 | const char* apk_path = apkPath.c_str(); | 
|  | 2202 | const char* instruction_set = instructionSet.c_str(); | 
|  | 2203 | const char* oat_dir = outputPath.c_str(); | 
|  | 2204 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2205 | bool res = delete_odex(apk_path, instruction_set, oat_dir); | 
|  | 2206 | return res ? ok() : error(); | 
| Andreas Gampe | 3964da0 | 2016-09-09 17:07:04 -0700 | [diff] [blame] | 2207 | } | 
|  | 2208 |  | 
| Calin Juravle | bd96836 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 2209 | binder::Status InstalldNativeService::reconcileSecondaryDexFile( | 
|  | 2210 | const std::string& dexPath, const std::string& packageName, int32_t uid, | 
|  | 2211 | const std::vector<std::string>& isas, const std::unique_ptr<std::string>& volumeUuid, | 
|  | 2212 | int32_t storage_flag, bool* _aidl_return) { | 
|  | 2213 | ENFORCE_UID(AID_SYSTEM); | 
|  | 2214 | CHECK_ARGUMENT_UUID(volumeUuid); | 
|  | 2215 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); | 
|  | 2216 |  | 
|  | 2217 | std::lock_guard<std::recursive_mutex> lock(mLock); | 
|  | 2218 | bool result = android::installd::reconcile_secondary_dex_file( | 
|  | 2219 | dexPath, packageName, uid, isas, volumeUuid, storage_flag, _aidl_return); | 
|  | 2220 | return result ? ok() : error(); | 
|  | 2221 | } | 
|  | 2222 |  | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2223 | binder::Status InstalldNativeService::invalidateMounts() { | 
|  | 2224 | ENFORCE_UID(AID_SYSTEM); | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2225 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2226 |  | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2227 | mStorageMounts.clear(); | 
|  | 2228 | mQuotaReverseMounts.clear(); | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2229 |  | 
|  | 2230 | std::ifstream in("/proc/mounts"); | 
|  | 2231 | if (!in.is_open()) { | 
|  | 2232 | return error("Failed to read mounts"); | 
|  | 2233 | } | 
|  | 2234 |  | 
|  | 2235 | std::string source; | 
|  | 2236 | std::string target; | 
|  | 2237 | std::string ignored; | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2238 | while (!in.eof()) { | 
|  | 2239 | std::getline(in, source, ' '); | 
|  | 2240 | std::getline(in, target, ' '); | 
|  | 2241 | std::getline(in, ignored); | 
|  | 2242 |  | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2243 | #if !BYPASS_SDCARDFS | 
|  | 2244 | if (target.compare(0, 21, "/mnt/runtime/default/") == 0) { | 
|  | 2245 | LOG(DEBUG) << "Found storage mount " << source << " at " << target; | 
|  | 2246 | mStorageMounts[source] = target; | 
|  | 2247 | } | 
|  | 2248 | #endif | 
|  | 2249 |  | 
|  | 2250 | #if !BYPASS_QUOTA | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2251 | if (source.compare(0, 11, "/dev/block/") == 0) { | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2252 | struct dqblk dq; | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2253 | if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0, | 
|  | 2254 | reinterpret_cast<char*>(&dq)) == 0) { | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2255 | LOG(DEBUG) << "Found quota mount " << source << " at " << target; | 
|  | 2256 | mQuotaReverseMounts[target] = source; | 
| Jeff Sharkey | e59c85c | 2017-04-02 21:53:14 -0600 | [diff] [blame] | 2257 |  | 
|  | 2258 | // ext4 only enables DQUOT_USAGE_ENABLED by default, so we | 
|  | 2259 | // need to kick it again to enable DQUOT_LIMITS_ENABLED. | 
|  | 2260 | if (quotactl(QCMD(Q_QUOTAON, USRQUOTA), source.c_str(), QFMT_VFS_V1, nullptr) != 0 | 
|  | 2261 | && errno != EBUSY) { | 
|  | 2262 | PLOG(ERROR) << "Failed to enable USRQUOTA on " << source; | 
|  | 2263 | } | 
|  | 2264 | if (quotactl(QCMD(Q_QUOTAON, GRPQUOTA), source.c_str(), QFMT_VFS_V1, nullptr) != 0 | 
|  | 2265 | && errno != EBUSY) { | 
|  | 2266 | PLOG(ERROR) << "Failed to enable GRPQUOTA on " << source; | 
|  | 2267 | } | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2268 | } | 
|  | 2269 | } | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2270 | #endif | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2271 | } | 
|  | 2272 | return ok(); | 
|  | 2273 | } | 
|  | 2274 |  | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2275 | std::string InstalldNativeService::findDataMediaPath( | 
|  | 2276 | const std::unique_ptr<std::string>& uuid, userid_t userid) { | 
|  | 2277 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); | 
|  | 2278 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; | 
|  | 2279 | auto path = StringPrintf("%s/media", create_data_path(uuid_).c_str()); | 
|  | 2280 | auto resolved = mStorageMounts[path]; | 
|  | 2281 | if (resolved.empty()) { | 
|  | 2282 | LOG(WARNING) << "Failed to find storage mount for " << path; | 
|  | 2283 | resolved = path; | 
|  | 2284 | } | 
|  | 2285 | return StringPrintf("%s/%u", resolved.c_str(), userid); | 
|  | 2286 | } | 
|  | 2287 |  | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2288 | std::string InstalldNativeService::findQuotaDeviceForUuid( | 
|  | 2289 | const std::unique_ptr<std::string>& uuid) { | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2290 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2291 | auto path = create_data_path(uuid ? uuid->c_str() : nullptr); | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2292 | return mQuotaReverseMounts[path]; | 
| Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2293 | } | 
|  | 2294 |  | 
| Jeff Sharkey | 325b8c9 | 2017-02-21 10:00:53 -0700 | [diff] [blame] | 2295 | binder::Status InstalldNativeService::isQuotaSupported( | 
|  | 2296 | const std::unique_ptr<std::string>& volumeUuid, bool* _aidl_return) { | 
|  | 2297 | *_aidl_return = !findQuotaDeviceForUuid(volumeUuid).empty(); | 
|  | 2298 | return ok(); | 
|  | 2299 | } | 
|  | 2300 |  | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 2301 | }  // namespace installd | 
|  | 2302 | }  // namespace android |