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