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