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