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