blob: f2ea0d30abf20c45839d8b725bb9fd6fb9e94c92 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** 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 Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** 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 Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Jeff Sharkeyf3e30b92016-12-09 17:06:57 -070017#include "InstalldNativeService.h"
Andreas Gampe02d0de52015-11-11 20:43:16 -080018
Jeff Sharkey466459b2017-01-17 15:25:01 -070019#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
20
Andreas Gampe02d0de52015-11-11 20:43:16 -080021#include <errno.h>
22#include <inttypes.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070023#include <fstream>
24#include <fts.h>
Andreas Gamped089ca12016-06-27 14:25:30 -070025#include <regex>
Andreas Gampe02d0de52015-11-11 20:43:16 -080026#include <stdlib.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070027#include <string.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080028#include <sys/capability.h>
29#include <sys/file.h>
30#include <sys/resource.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070031#include <sys/quota.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080032#include <sys/stat.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070033#include <sys/statvfs.h>
Jeff Sharkeycc6281c2016-02-06 19:46:09 -070034#include <sys/types.h>
Calin Juravle6a1648e2016-02-01 12:12:16 +000035#include <sys/wait.h>
Jeff Sharkeycc6281c2016-02-06 19:46:09 -070036#include <sys/xattr.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080037#include <unistd.h>
Jeff Sharkey41ea4242015-04-09 11:34:03 -070038
Andreas Gampe6fb5a012016-06-03 16:09:32 -070039#include <android-base/logging.h>
Elliott Hughese4ec9eb2015-12-04 15:39:32 -080040#include <android-base/stringprintf.h>
David Sehr6727c2d2016-05-17 16:06:22 -070041#include <android-base/strings.h>
Roland Levillain64b59cc2016-04-05 16:41:12 +010042#include <android-base/unique_fd.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080043#include <cutils/fs.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070044#include <cutils/properties.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070045#include <cutils/sched_policy.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070046#include <log/log.h> // TODO: Move everything to base/logging.
Jeff Sharkeye3637242015-04-08 20:56:42 -070047#include <logwrap/logwrap.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080048#include <private/android_filesystem_config.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070049#include <selinux/android.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080050#include <system/thread_defs.h>
Jeff Sharkey466459b2017-01-17 15:25:01 -070051#include <utils/Trace.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070052
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070053#include "dexopt.h"
Jeff Sharkeyf3e30b92016-12-09 17:06:57 -070054#include "globals.h"
55#include "installd_deps.h"
56#include "otapreopt_utils.h"
57#include "utils.h"
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070058
Jeff Sharkey88ddd942017-01-17 18:05:54 -070059#include "CacheTracker.h"
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070060#include "MatchExtensionGen.h"
61
Andreas Gampe02d0de52015-11-11 20:43:16 -080062#ifndef LOG_TAG
63#define LOG_TAG "installd"
64#endif
Jeff Sharkey41ea4242015-04-09 11:34:03 -070065
66using android::base::StringPrintf;
Jeff Sharkey66b1a122017-01-16 20:57:45 -070067using std::endl;
Mike Lockwood94afecf2012-10-24 10:45:23 -070068
Andreas Gampe02d0de52015-11-11 20:43:16 -080069namespace android {
70namespace installd {
Mike Lockwood94afecf2012-10-24 10:45:23 -070071
Jeff Sharkeycc6281c2016-02-06 19:46:09 -070072static constexpr const char* kCpPath = "/system/bin/cp";
73static constexpr const char* kXattrDefault = "user.default";
Jeff Sharkeye3637242015-04-08 20:56:42 -070074
Andreas Gamped089ca12016-06-27 14:25:30 -070075static constexpr const char* PKG_LIB_POSTFIX = "/lib";
76static constexpr const char* CACHE_DIR_POSTFIX = "/cache";
77static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache";
78
79static constexpr const char* IDMAP_PREFIX = "/data/resource-cache/";
80static constexpr const char* IDMAP_SUFFIX = "@idmap";
81
Andreas Gamped089ca12016-06-27 14:25:30 -070082// NOTE: keep in sync with Installer
83static constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8;
84static constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070085static constexpr int FLAG_USE_QUOTA = 1 << 12;
Jeff Sharkey88ddd942017-01-17 18:05:54 -070086static constexpr int FLAG_FREE_CACHE_V2 = 1 << 13;
87static constexpr int FLAG_FREE_CACHE_NOOP = 1 << 14;
Andreas Gamped089ca12016-06-27 14:25:30 -070088
Janis Danisevskiseecb2d22016-01-12 14:45:55 +000089#define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M
Jeff Sharkey0274c972016-12-06 09:32:04 -070090namespace {
91
92constexpr const char* kDump = "android.permission.DUMP";
93
Jeff Sharkey423e7462016-12-09 18:18:43 -070094static binder::Status ok() {
95 return binder::Status::ok();
96}
97
98static binder::Status exception(uint32_t code, const std::string& msg) {
99 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
100}
101
102static binder::Status error() {
103 return binder::Status::fromServiceSpecificError(errno);
104}
105
106static binder::Status error(const std::string& msg) {
107 PLOG(ERROR) << msg;
108 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
109}
110
111static binder::Status error(uint32_t code, const std::string& msg) {
112 LOG(ERROR) << msg << " (" << code << ")";
113 return binder::Status::fromServiceSpecificError(code, String8(msg.c_str()));
114}
115
Jeff Sharkey0274c972016-12-06 09:32:04 -0700116binder::Status checkPermission(const char* permission) {
117 pid_t pid;
118 uid_t uid;
119
120 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
121 reinterpret_cast<int32_t*>(&uid))) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700122 return ok();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700123 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700124 return exception(binder::Status::EX_SECURITY,
125 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
Jeff Sharkey0274c972016-12-06 09:32:04 -0700126 }
127}
128
129binder::Status checkUid(uid_t expectedUid) {
130 uid_t uid = IPCThreadState::self()->getCallingUid();
131 if (uid == expectedUid || uid == AID_ROOT) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700132 return ok();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700133 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700134 return exception(binder::Status::EX_SECURITY,
135 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
136 }
137}
138
139binder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) {
140 if (!uuid || is_valid_filename(*uuid)) {
141 return ok();
142 } else {
143 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
144 StringPrintf("UUID %s is malformed", uuid->c_str()));
145 }
146}
147
148binder::Status checkArgumentPackageName(const std::string& packageName) {
149 if (is_valid_package_name(packageName.c_str())) {
150 return ok();
151 } else {
152 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
153 StringPrintf("Package name %s is malformed", packageName.c_str()));
Jeff Sharkey0274c972016-12-06 09:32:04 -0700154 }
155}
156
157#define ENFORCE_UID(uid) { \
158 binder::Status status = checkUid((uid)); \
159 if (!status.isOk()) { \
160 return status; \
161 } \
162}
163
Jeff Sharkey423e7462016-12-09 18:18:43 -0700164#define CHECK_ARGUMENT_UUID(uuid) { \
165 binder::Status status = checkArgumentUuid((uuid)); \
166 if (!status.isOk()) { \
167 return status; \
168 } \
169}
170
171#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \
172 binder::Status status = \
173 checkArgumentPackageName((packageName)); \
174 if (!status.isOk()) { \
175 return status; \
176 } \
177}
178
Jeff Sharkey0274c972016-12-06 09:32:04 -0700179} // namespace
180
181status_t InstalldNativeService::start() {
182 IPCThreadState::self()->disableBackgroundScheduling(true);
183 status_t ret = BinderService<InstalldNativeService>::publish();
184 if (ret != android::OK) {
185 return ret;
186 }
187 sp<ProcessState> ps(ProcessState::self());
188 ps->startThreadPool();
189 ps->giveThreadPoolName();
190 return android::OK;
191}
192
193status_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) {
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700194 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
Jeff Sharkey0274c972016-12-06 09:32:04 -0700195 const binder::Status dump_permission = checkPermission(kDump);
196 if (!dump_permission.isOk()) {
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700197 out << dump_permission.toString8() << endl;
Jeff Sharkey0274c972016-12-06 09:32:04 -0700198 return PERMISSION_DENIED;
199 }
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700200 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700201
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700202 out << "installd is happy!" << endl;
203
204 out << endl << "Devices with quota support:" << endl;
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700205 for (const auto& n : mQuotaDevices) {
206 out << " " << n.first << " = " << n.second << endl;
207 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700208
209 out << endl << "Per-UID cache quotas:" << endl;
210 for (const auto& n : mCacheQuotas) {
211 out << " " << n.first << " = " << n.second << endl;
212 }
213
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700214 out << endl;
215 out.flush();
216
Jeff Sharkey0274c972016-12-06 09:32:04 -0700217 return NO_ERROR;
218}
219
Jeff Sharkey7db60412016-09-20 18:21:42 -0600220/**
221 * Perform restorecon of the given path, but only perform recursive restorecon
222 * if the label of that top-level file actually changed. This can save us
223 * significant time by avoiding no-op traversals of large filesystem trees.
224 */
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700225static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid,
226 bool existing) {
Jeff Sharkey7db60412016-09-20 18:21:42 -0600227 int res = 0;
228 char* before = nullptr;
229 char* after = nullptr;
230
231 // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by
232 // libselinux. Not needed here.
233
Jeff Sharkey95d9ac62016-10-31 11:22:19 -0600234 if (lgetfilecon(path.c_str(), &before) < 0) {
Jeff Sharkey7db60412016-09-20 18:21:42 -0600235 PLOG(ERROR) << "Failed before getfilecon for " << path;
236 goto fail;
237 }
Jeff Sharkey0274c972016-12-06 09:32:04 -0700238 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) {
Jeff Sharkey7db60412016-09-20 18:21:42 -0600239 PLOG(ERROR) << "Failed top-level restorecon for " << path;
240 goto fail;
241 }
Jeff Sharkey95d9ac62016-10-31 11:22:19 -0600242 if (lgetfilecon(path.c_str(), &after) < 0) {
Jeff Sharkey7db60412016-09-20 18:21:42 -0600243 PLOG(ERROR) << "Failed after getfilecon for " << path;
244 goto fail;
245 }
246
247 // If the initial top-level restorecon above changed the label, then go
248 // back and restorecon everything recursively
249 if (strcmp(before, after)) {
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700250 if (existing) {
251 LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at "
252 << path << "; running recursive restorecon";
253 }
Jeff Sharkey0274c972016-12-06 09:32:04 -0700254 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid,
Jeff Sharkey7db60412016-09-20 18:21:42 -0600255 SELINUX_ANDROID_RESTORECON_RECURSE) < 0) {
256 PLOG(ERROR) << "Failed recursive restorecon for " << path;
257 goto fail;
258 }
259 }
260
261 goto done;
262fail:
263 res = -1;
264done:
265 free(before);
266 free(after);
267 return res;
268}
269
Jeff Sharkey0274c972016-12-06 09:32:04 -0700270static int restorecon_app_data_lazy(const std::string& parent, const char* name,
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700271 const std::string& seInfo, uid_t uid, bool existing) {
272 return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid,
273 existing);
Jeff Sharkey95d9ac62016-10-31 11:22:19 -0600274}
275
Jeff Sharkey7db60412016-09-20 18:21:42 -0600276static int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) {
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600277 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) {
278 PLOG(ERROR) << "Failed to prepare " << path;
279 return -1;
280 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600281 return 0;
282}
283
Jeff Sharkey0274c972016-12-06 09:32:04 -0700284binder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid,
285 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700286 const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700287 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700288 CHECK_ARGUMENT_UUID(uuid);
289 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700290 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700291
292 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
293 const char* pkgname = packageName.c_str();
294
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700295 // Assume invalid inode unless filled in below
296 if (_aidl_return != nullptr) *_aidl_return = -1;
297
Jeff Sharkey24ef15b2017-01-12 19:27:03 -0700298 int32_t uid = multiuser_get_uid(userId, appId);
299 int32_t cacheGid = multiuser_get_cache_gid(userId, appId);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700300 mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751;
301
Jeff Sharkey24ef15b2017-01-12 19:27:03 -0700302 // If UID doesn't have a specific cache GID, use UID value
303 if (cacheGid == -1) {
304 cacheGid = uid;
305 }
306
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700307 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700308 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname);
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700309 bool existing = (access(path.c_str(), F_OK) == 0);
310
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700311 if (prepare_app_dir(path, targetMode, uid) ||
312 prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) ||
313 prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700314 return error("Failed to prepare " + path);
Jeff Sharkey7db60412016-09-20 18:21:42 -0600315 }
316
317 // Consider restorecon over contents if label changed
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700318 if (restorecon_app_data_lazy(path, seInfo, uid, existing) ||
319 restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) ||
320 restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700321 return error("Failed to restorecon " + path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700322 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600323
324 // Remember inode numbers of cache directories so that we can clear
325 // contents while CE storage is locked
326 if (write_path_inode(path, "cache", kXattrInodeCache) ||
327 write_path_inode(path, "code_cache", kXattrInodeCodeCache)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700328 return error("Failed to write_path_inode for " + path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700329 }
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700330
331 // And return the CE inode of the top-level data directory so we can
332 // clear contents while CE storage is locked
333 if ((_aidl_return != nullptr)
334 && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) {
335 return error("Failed to get_path_inode for " + path);
336 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700337 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700338 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700339 auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700340 bool existing = (access(path.c_str(), F_OK) == 0);
341
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700342 if (prepare_app_dir(path, targetMode, uid) ||
343 prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) ||
344 prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700345 return error("Failed to prepare " + path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700346 }
Calin Juravle6a1648e2016-02-01 12:12:16 +0000347
Jeff Sharkey7db60412016-09-20 18:21:42 -0600348 // Consider restorecon over contents if label changed
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700349 if (restorecon_app_data_lazy(path, seInfo, uid, existing)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700350 return error("Failed to restorecon " + path);
Jeff Sharkey7db60412016-09-20 18:21:42 -0600351 }
352
Jeff Sharkey90aff262016-12-12 14:28:24 -0700353 if (property_get_bool("dalvik.vm.usejitprofiles", false)) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700354 const std::string profile_path = create_data_user_profile_package_path(userId, pkgname);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000355 // read-write-execute only for the app user.
356 if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700357 return error("Failed to prepare " + profile_path);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000358 }
Calin Juravlebc56e7d2016-05-24 15:33:12 +0100359 std::string profile_file = create_primary_profile(profile_path);
360 // read-write only for the app user.
361 if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700362 return error("Failed to prepare " + profile_path);
Calin Juravlebc56e7d2016-05-24 15:33:12 +0100363 }
Calin Juravle6a1648e2016-02-01 12:12:16 +0000364 const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname);
365 // dex2oat/profman runs under the shared app gid and it needs to read/write reference
366 // profiles.
Jeff Sharkeyadddd982017-01-03 14:33:50 -0700367 int shared_app_gid = multiuser_get_shared_gid(0, appId);
Jeff Sharkey2e6dc9b2016-12-15 14:50:11 -0700368 if ((shared_app_gid != -1) && fs_prepare_dir_strict(
Calin Juravle6a1648e2016-02-01 12:12:16 +0000369 ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700370 return error("Failed to prepare " + ref_profile_path);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000371 }
372 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700373 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700374 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700375}
376
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700377binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid,
378 const std::string& packageName, int32_t userId, int32_t flags) {
379 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700380 CHECK_ARGUMENT_UUID(uuid);
381 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700382 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700383
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700384 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
385 const char* pkgname = packageName.c_str();
386
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700387 // This method only exists to upgrade system apps that have requested
388 // forceDeviceEncrypted, so their default storage always lives in a
389 // consistent location. This only works on non-FBE devices, since we
390 // never want to risk exposing data on a device with real CE/DE storage.
391
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700392 auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname);
393 auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700394
395 // If neither directory is marked as default, assume CE is default
396 if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1
397 && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) {
398 if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700399 return error("Failed to mark default storage " + ce_path);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700400 }
401 }
402
403 // Migrate default data location if needed
404 auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path;
405 auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path;
406
407 if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) {
408 LOG(WARNING) << "Requested default storage " << target
409 << " is not active; migrating from " << source;
410 if (delete_dir_contents_and_dir(target) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700411 return error("Failed to delete " + target);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700412 }
413 if (rename(source.c_str(), target.c_str()) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700414 return error("Failed to rename " + source + " to " + target);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700415 }
416 }
417
Jeff Sharkey423e7462016-12-09 18:18:43 -0700418 return ok();
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700419}
420
Calin Juravle6a1648e2016-02-01 12:12:16 +0000421
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700422binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) {
423 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700424 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700425 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700426
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700427 const char* pkgname = packageName.c_str();
Jeff Sharkey423e7462016-12-09 18:18:43 -0700428 binder::Status res = ok();
429 if (!clear_reference_profile(pkgname)) {
430 res = error("Failed to clear reference profile for " + packageName);
431 }
432 if (!clear_current_profiles(pkgname)) {
433 res = error("Failed to clear current profiles for " + packageName);
434 }
435 return res;
Calin Juravle6a1648e2016-02-01 12:12:16 +0000436}
437
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700438binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid,
439 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
440 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700441 CHECK_ARGUMENT_UUID(uuid);
442 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700443 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700444
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700445 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
446 const char* pkgname = packageName.c_str();
447
Jeff Sharkey423e7462016-12-09 18:18:43 -0700448 binder::Status res = ok();
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700449 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700450 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600451 if (flags & FLAG_CLEAR_CACHE_ONLY) {
452 path = read_path_inode(path, "cache", kXattrInodeCache);
453 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
454 path = read_path_inode(path, "code_cache", kXattrInodeCodeCache);
455 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700456 if (access(path.c_str(), F_OK) == 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700457 if (delete_dir_contents(path) != 0) {
458 res = error("Failed to delete contents of " + path);
459 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700460 }
461 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700462 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600463 std::string suffix = "";
464 bool only_cache = false;
465 if (flags & FLAG_CLEAR_CACHE_ONLY) {
466 suffix = CACHE_DIR_POSTFIX;
467 only_cache = true;
468 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
469 suffix = CODE_CACHE_DIR_POSTFIX;
470 only_cache = true;
471 }
472
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700473 auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix;
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700474 if (access(path.c_str(), F_OK) == 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700475 if (delete_dir_contents(path) != 0) {
476 res = error("Failed to delete contents of " + path);
477 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700478 }
Calin Juravleedae6692016-03-23 13:55:31 +0000479 if (!only_cache) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700480 if (!clear_current_profile(pkgname, userId)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700481 res = error("Failed to clear current profile for " + packageName);
Calin Juravleedae6692016-03-23 13:55:31 +0000482 }
483 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700484 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700485 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700486}
487
Calin Juravle7535e8e2016-03-29 16:05:40 +0100488static int destroy_app_reference_profile(const char *pkgname) {
489 return delete_dir_contents_and_dir(
490 create_data_ref_profile_package_path(pkgname),
491 /*ignore_if_missing*/ true);
492}
493
Calin Juravleedae6692016-03-23 13:55:31 +0000494static int destroy_app_current_profiles(const char *pkgname, userid_t userid) {
Calin Juravleb06f98a2016-03-28 15:11:01 +0100495 return delete_dir_contents_and_dir(
496 create_data_user_profile_package_path(userid, pkgname),
497 /*ignore_if_missing*/ true);
Calin Juravleedae6692016-03-23 13:55:31 +0000498}
499
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700500binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) {
501 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700502 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700503 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700504
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700505 const char* pkgname = packageName.c_str();
Jeff Sharkey423e7462016-12-09 18:18:43 -0700506 binder::Status res = ok();
Calin Juravleedae6692016-03-23 13:55:31 +0000507 std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
508 for (auto user : users) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700509 if (destroy_app_current_profiles(pkgname, user) != 0) {
510 res = error("Failed to destroy current profiles for " + packageName);
511 }
Calin Juravleedae6692016-03-23 13:55:31 +0000512 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700513 if (destroy_app_reference_profile(pkgname) != 0) {
514 res = error("Failed to destroy reference profile for " + packageName);
515 }
516 return res;
Calin Juravlecaa6b802016-03-22 14:15:52 +0000517}
518
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700519binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid,
520 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
521 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700522 CHECK_ARGUMENT_UUID(uuid);
523 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700524 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700525
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700526 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
527 const char* pkgname = packageName.c_str();
528
Jeff Sharkey423e7462016-12-09 18:18:43 -0700529 binder::Status res = ok();
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700530 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700531 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
532 if (delete_dir_contents_and_dir(path) != 0) {
533 res = error("Failed to delete " + path);
534 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700535 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700536 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700537 auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
538 if (delete_dir_contents_and_dir(path) != 0) {
539 res = error("Failed to delete " + path);
540 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700541 destroy_app_current_profiles(pkgname, userId);
Calin Juravle7535e8e2016-03-29 16:05:40 +0100542 // TODO(calin): If the package is still installed by other users it's probably
543 // beneficial to keep the reference profile around.
544 // Verify if it's ok to do that.
545 destroy_app_reference_profile(pkgname);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700546 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700547 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700548}
549
Jeff Sharkey0274c972016-12-06 09:32:04 -0700550binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid,
551 const std::unique_ptr<std::string>& toUuid, const std::string& packageName,
552 const std::string& dataAppName, int32_t appId, const std::string& seInfo,
553 int32_t targetSdkVersion) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700554 ENFORCE_UID(AID_SYSTEM);
555 CHECK_ARGUMENT_UUID(fromUuid);
556 CHECK_ARGUMENT_UUID(toUuid);
557 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700558 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700559
560 const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr;
561 const char* to_uuid = toUuid ? toUuid->c_str() : nullptr;
562 const char* package_name = packageName.c_str();
563 const char* data_app_name = dataAppName.c_str();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700564
Jeff Sharkey423e7462016-12-09 18:18:43 -0700565 binder::Status res = ok();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700566 std::vector<userid_t> users = get_known_users(from_uuid);
567
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700568 // Copy app
569 {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600570 auto from = create_data_app_package_path(from_uuid, data_app_name);
571 auto to = create_data_app_package_path(to_uuid, data_app_name);
572 auto to_parent = create_data_app_path(to_uuid);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700573
574 char *argv[] = {
575 (char*) kCpPath,
576 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
577 (char*) "-p", /* preserve timestamps, ownership, and permissions */
578 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
579 (char*) "-P", /* Do not follow symlinks [default] */
580 (char*) "-d", /* don't dereference symlinks */
581 (char*) from.c_str(),
582 (char*) to_parent.c_str()
583 };
584
585 LOG(DEBUG) << "Copying " << from << " to " << to;
586 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700587 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700588 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700589 goto fail;
590 }
591
592 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700593 res = error("Failed to restorecon " + to);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700594 goto fail;
595 }
596 }
597
598 // Copy private data for all known users
Jeff Sharkeye3637242015-04-08 20:56:42 -0700599 for (auto user : users) {
Jeff Sharkeye3637242015-04-08 20:56:42 -0700600
601 // Data source may not exist for all users; that's okay
Jeff Sharkey51c94492016-05-10 17:46:39 -0600602 auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name);
603 if (access(from_ce.c_str(), F_OK) != 0) {
604 LOG(INFO) << "Missing source " << from_ce;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700605 continue;
606 }
607
Jeff Sharkey0274c972016-12-06 09:32:04 -0700608 if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId,
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700609 seInfo, targetSdkVersion, nullptr).isOk()) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700610 res = error("Failed to create package target");
Jeff Sharkeye3637242015-04-08 20:56:42 -0700611 goto fail;
612 }
613
614 char *argv[] = {
615 (char*) kCpPath,
616 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
617 (char*) "-p", /* preserve timestamps, ownership, and permissions */
618 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
619 (char*) "-P", /* Do not follow symlinks [default] */
620 (char*) "-d", /* don't dereference symlinks */
Jeff Sharkey51c94492016-05-10 17:46:39 -0600621 nullptr,
622 nullptr
Jeff Sharkeye3637242015-04-08 20:56:42 -0700623 };
624
Jeff Sharkey51c94492016-05-10 17:46:39 -0600625 {
626 auto from = create_data_user_de_package_path(from_uuid, user, package_name);
627 auto to = create_data_user_de_path(to_uuid, user);
628 argv[6] = (char*) from.c_str();
629 argv[7] = (char*) to.c_str();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700630
Jeff Sharkey51c94492016-05-10 17:46:39 -0600631 LOG(DEBUG) << "Copying " << from << " to " << to;
632 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
633 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700634 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkey51c94492016-05-10 17:46:39 -0600635 goto fail;
636 }
637 }
638 {
639 auto from = create_data_user_ce_package_path(from_uuid, user, package_name);
640 auto to = create_data_user_ce_path(to_uuid, user);
641 argv[6] = (char*) from.c_str();
642 argv[7] = (char*) to.c_str();
643
644 LOG(DEBUG) << "Copying " << from << " to " << to;
645 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
646 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700647 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkey51c94492016-05-10 17:46:39 -0600648 goto fail;
649 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700650 }
651
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700652 if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE,
653 appId, seInfo).isOk()) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700654 res = error("Failed to restorecon");
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700655 goto fail;
656 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700657 }
658
Jeff Sharkey31f08982015-07-07 13:31:37 -0700659 // We let the framework scan the new location and persist that before
660 // deleting the data in the old location; this ordering ensures that
661 // we can recover from things like battery pulls.
Jeff Sharkey423e7462016-12-09 18:18:43 -0700662 return ok();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700663
664fail:
665 // Nuke everything we might have already copied
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700666 {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600667 auto to = create_data_app_package_path(to_uuid, data_app_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700668 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
669 LOG(WARNING) << "Failed to rollback " << to;
670 }
671 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700672 for (auto user : users) {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600673 {
674 auto to = create_data_user_de_package_path(to_uuid, user, package_name);
675 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
676 LOG(WARNING) << "Failed to rollback " << to;
677 }
678 }
679 {
680 auto to = create_data_user_ce_package_path(to_uuid, user, package_name);
681 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
682 LOG(WARNING) << "Failed to rollback " << to;
683 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700684 }
685 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700686 return res;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700687}
688
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700689binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid,
690 int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) {
691 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700692 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700693 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700694
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700695 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600696 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700697 if (uuid_ == nullptr) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700698 if (ensure_config_user_dirs(userId) != 0) {
Jeff Sharkey7be5ead2016-12-12 13:18:46 -0700699 return error(StringPrintf("Failed to ensure dirs for %d", userId));
Jeff Sharkey423e7462016-12-09 18:18:43 -0700700 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700701 }
702 }
Jeff Sharkey7be5ead2016-12-12 13:18:46 -0700703 return ok();
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700704}
705
706binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid,
707 int32_t userId, int32_t flags) {
708 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700709 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700710 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700711
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700712 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey423e7462016-12-09 18:18:43 -0700713 binder::Status res = ok();
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700714 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700715 auto path = create_data_user_de_path(uuid_, userId);
716 if (delete_dir_contents_and_dir(path, true) != 0) {
717 res = error("Failed to delete " + path);
718 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700719 if (uuid_ == nullptr) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700720 path = create_data_misc_legacy_path(userId);
721 if (delete_dir_contents_and_dir(path, true) != 0) {
722 res = error("Failed to delete " + path);
723 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700724 path = create_data_user_profile_path(userId);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700725 if (delete_dir_contents_and_dir(path, true) != 0) {
726 res = error("Failed to delete " + path);
727 }
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700728 }
Robin Lee095c7632014-04-25 15:05:19 +0100729 }
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600730 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700731 auto path = create_data_user_ce_path(uuid_, userId);
732 if (delete_dir_contents_and_dir(path, true) != 0) {
733 res = error("Failed to delete " + path);
734 }
735 path = create_data_media_path(uuid_, userId);
736 if (delete_dir_contents_and_dir(path, true) != 0) {
737 res = error("Failed to delete " + path);
738 }
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600739 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700740 return res;
Robin Lee095c7632014-04-25 15:05:19 +0100741}
742
Mike Lockwood94afecf2012-10-24 10:45:23 -0700743/* Try to ensure free_size bytes of storage are available.
744 * Returns 0 on success.
745 * This is rather simple-minded because doing a full LRU would
746 * be potentially memory-intensive, and without atime it would
747 * also require that apps constantly modify file metadata even
748 * when just reading from the cache, which is pretty awful.
749 */
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700750binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid,
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700751 int64_t freeStorageSize, int32_t flags) {
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700752 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700753 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700754 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700755
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700756 // TODO: remove this once framework is more robust
757 invalidateMounts();
758
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700759 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700760 auto data_path = create_data_path(uuid_);
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700761 auto device = findQuotaDeviceForUuid(uuid);
762 auto noop = (flags & FLAG_FREE_CACHE_NOOP);
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700763
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700764 int64_t free = data_disk_free(data_path);
765 int64_t needed = freeStorageSize - free;
766 if (free < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700767 return error("Failed to determine free space for " + data_path);
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700768 } else if (free >= freeStorageSize) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700769 return ok();
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700770 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700771
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700772 LOG(DEBUG) << "Found " << data_path << " with " << free << " free; caller requested "
773 << freeStorageSize;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700774
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700775 if (flags & FLAG_FREE_CACHE_V2) {
776 // This new cache strategy fairly removes files from UIDs by deleting
777 // files from the UIDs which are most over their allocated quota
778
779 // 1. Create trackers for every known UID
780 ATRACE_BEGIN("create");
781 std::unordered_map<uid_t, std::shared_ptr<CacheTracker>> trackers;
782 for (auto user : get_known_users(uuid_)) {
783 FTS *fts;
784 FTSENT *p;
785 char *argv[] = {
786 (char*) create_data_user_ce_path(uuid_, user).c_str(),
787 (char*) create_data_user_de_path(uuid_, user).c_str(),
788 nullptr
789 };
790 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) {
791 return error("Failed to fts_open");
792 }
793 while ((p = fts_read(fts)) != NULL) {
794 if (p->fts_info == FTS_D && p->fts_level == 1) {
795 uid_t uid = p->fts_statp->st_uid;
796 auto search = trackers.find(uid);
797 if (search != trackers.end()) {
798 search->second->addDataPath(p->fts_path);
799 } else {
800 auto tracker = std::shared_ptr<CacheTracker>(new CacheTracker(
801 multiuser_get_user_id(uid), multiuser_get_app_id(uid), device));
802 tracker->addDataPath(p->fts_path);
803 tracker->cacheQuota = mCacheQuotas[uid];
804 if (tracker->cacheQuota == 0) {
805 LOG(WARNING) << "UID " << uid << " has no cache quota; assuming 64MB";
806 tracker->cacheQuota = 67108864;
807 }
808 trackers[uid] = tracker;
809 }
810 fts_set(fts, p, FTS_SKIP);
811 }
812 }
813 fts_close(fts);
814 }
815 ATRACE_END();
816
817 // 2. Populate tracker stats and insert into priority queue
818 ATRACE_BEGIN("populate");
819 auto cmp = [](std::shared_ptr<CacheTracker> left, std::shared_ptr<CacheTracker> right) {
820 return (left->getCacheRatio() < right->getCacheRatio());
821 };
822 std::priority_queue<std::shared_ptr<CacheTracker>,
823 std::vector<std::shared_ptr<CacheTracker>>, decltype(cmp)> queue(cmp);
824 for (const auto& it : trackers) {
825 it.second->loadStats();
826 queue.push(it.second);
827 }
828 ATRACE_END();
829
830 // 3. Bounce across the queue, freeing items from whichever tracker is
831 // the most over their assigned quota
832 ATRACE_BEGIN("bounce");
833 std::shared_ptr<CacheTracker> active;
834 while (active || !queue.empty()) {
835 // Find the best tracker to work with; this might involve swapping
836 // if the active tracker is no longer the most over quota
837 bool nextBetter = active && !queue.empty()
838 && active->getCacheRatio() < queue.top()->getCacheRatio();
839 if (!active || nextBetter) {
840 if (active) {
841 // Current tracker still has items, so we'll consider it
842 // again later once it bubbles up to surface
843 queue.push(active);
844 }
845 active = queue.top(); queue.pop();
846 active->ensureItems();
847 continue;
848 }
849
850 // If no items remain, go find another tracker
851 if (active->items.empty()) {
852 active = nullptr;
853 continue;
854 } else {
855 auto item = active->items.back();
856 active->items.pop_back();
857
858 LOG(DEBUG) << "Purging " << item->toString() << " from " << active->toString();
859 if (!noop) {
860 item->purge();
861 }
862 active->cacheUsed -= item->size;
863 needed -= item->size;
864 }
865
866 // Verify that we're actually done before bailing, since sneaky
867 // apps might be using hardlinks
868 if (needed <= 0) {
869 free = data_disk_free(data_path);
870 needed = freeStorageSize - free;
871 if (needed <= 0) {
872 break;
873 } else {
874 LOG(WARNING) << "Expected to be done but still need " << needed;
875 }
876 }
877 }
878 ATRACE_END();
879
880 } else {
881 ATRACE_BEGIN("start");
882 cache_t* cache = start_cache_collection();
883 ATRACE_END();
884
885 ATRACE_BEGIN("add");
886 for (auto user : get_known_users(uuid_)) {
887 add_cache_files(cache, create_data_user_ce_path(uuid_, user));
888 add_cache_files(cache, create_data_user_de_path(uuid_, user));
889 add_cache_files(cache,
890 StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str()));
891 }
892 ATRACE_END();
893
894 ATRACE_BEGIN("clear");
895 clear_cache_files(data_path, cache, freeStorageSize);
896 ATRACE_END();
897
898 ATRACE_BEGIN("finish");
899 finish_cache_collection(cache);
900 ATRACE_END();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700901 }
902
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700903 free = data_disk_free(data_path);
904 if (free >= freeStorageSize) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700905 return ok();
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700906 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700907 return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64,
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700908 freeStorageSize, data_path.c_str(), free));
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700909 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700910}
911
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700912binder::Status InstalldNativeService::rmdex(const std::string& codePath,
913 const std::string& instructionSet) {
914 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700915 std::lock_guard<std::recursive_mutex> lock(mLock);
916
Mike Lockwood94afecf2012-10-24 10:45:23 -0700917 char dex_path[PKG_PATH_MAX];
918
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700919 const char* path = codePath.c_str();
920 const char* instruction_set = instructionSet.c_str();
921
Jeff Sharkey770180a2014-09-08 17:14:26 -0700922 if (validate_apk_path(path) && validate_system_app_path(path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700923 return error("Invalid path " + codePath);
Jeff Sharkey770180a2014-09-08 17:14:26 -0700924 }
925
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700926 if (!create_cache_path(dex_path, path, instruction_set)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700927 return error("Failed to create cache path for " + codePath);
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700928 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700929
930 ALOGV("unlink %s\n", dex_path);
931 if (unlink(dex_path) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700932 return error(StringPrintf("Failed to unlink %s", dex_path));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700933 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700934 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700935 }
936}
937
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700938struct stats {
939 int64_t codeSize;
940 int64_t dataSize;
941 int64_t cacheSize;
942};
943
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700944#if MEASURE_DEBUG
945static std::string toString(std::vector<int64_t> values) {
946 std::stringstream res;
947 res << "[";
948 for (size_t i = 0; i < values.size(); i++) {
949 res << values[i];
950 if (i < values.size() - 1) {
951 res << ",";
952 }
953 }
954 res << "]";
955 return res.str();
956}
957#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700958
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700959static void collectQuotaStats(const std::string& device, int32_t userId,
Jeff Sharkey7bf5b952017-01-19 09:21:36 -0700960 int32_t appId, struct stats* stats, struct stats* extStats) {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700961 if (device.empty()) return;
962
963 struct dqblk dq;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700964
965 uid_t uid = multiuser_get_uid(userId, appId);
966 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
967 reinterpret_cast<char*>(&dq)) != 0) {
968 if (errno != ESRCH) {
969 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
970 }
971 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700972#if MEASURE_DEBUG
973 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
974#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700975 stats->dataSize += dq.dqb_curspace;
976 }
977
978 int cacheGid = multiuser_get_cache_gid(userId, appId);
979 if (cacheGid != -1) {
980 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid,
981 reinterpret_cast<char*>(&dq)) != 0) {
982 if (errno != ESRCH) {
983 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid;
984 }
985 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700986#if MEASURE_DEBUG
Jeff Sharkey7bf5b952017-01-19 09:21:36 -0700987 LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700988#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700989 stats->cacheSize += dq.dqb_curspace;
990 }
991 }
992
Jeff Sharkey7bf5b952017-01-19 09:21:36 -0700993 int extGid = multiuser_get_ext_gid(userId, appId);
994 if (extGid != -1) {
995 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), extGid,
996 reinterpret_cast<char*>(&dq)) != 0) {
997 if (errno != ESRCH) {
998 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << extGid;
999 }
1000 } else {
1001#if MEASURE_DEBUG
1002 LOG(DEBUG) << "quotactl() for GID " << extGid << " " << dq.dqb_curspace;
1003#endif
1004 extStats->dataSize += dq.dqb_curspace;
1005 }
1006 }
1007
1008 int sharedGid = multiuser_get_shared_gid(userId, appId);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001009 if (sharedGid != -1) {
1010 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid,
1011 reinterpret_cast<char*>(&dq)) != 0) {
1012 if (errno != ESRCH) {
1013 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid;
1014 }
1015 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001016#if MEASURE_DEBUG
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001017 LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001018#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001019 stats->codeSize += dq.dqb_curspace;
1020 }
1021 }
1022}
1023
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001024static void collectManualStats(const std::string& path, struct stats* stats) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001025 DIR *d;
1026 int dfd;
1027 struct dirent *de;
1028 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001029
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001030 d = opendir(path.c_str());
1031 if (d == nullptr) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001032 if (errno != ENOENT) {
1033 PLOG(WARNING) << "Failed to open " << path;
1034 }
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001035 return;
1036 }
1037 dfd = dirfd(d);
1038 while ((de = readdir(d))) {
1039 const char *name = de->d_name;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001040
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001041 int64_t size = 0;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001042 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001043 size = s.st_blocks * 512;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001044 }
1045
1046 if (de->d_type == DT_DIR) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001047 if (!strcmp(name, ".")) {
1048 // Don't recurse, but still count node size
1049 } else if (!strcmp(name, "..")) {
1050 // Don't recurse or count node size
1051 continue;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001052 } else {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001053 // Measure all children nodes
1054 size = 0;
1055 calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001056 }
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001057
1058 if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) {
1059 stats->cacheSize += size;
1060 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001061 }
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001062
1063 // Legacy symlink isn't owned by app
1064 if (de->d_type == DT_LNK && !strcmp(name, "lib")) {
1065 continue;
1066 }
1067
1068 // Everything found inside is considered data
1069 stats->dataSize += size;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001070 }
1071 closedir(d);
1072}
1073
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001074static void collectManualStatsForUser(const std::string& path, struct stats* stats,
1075 bool exclude_apps = false) {
1076 DIR *d;
1077 int dfd;
1078 struct dirent *de;
1079 struct stat s;
1080
1081 d = opendir(path.c_str());
1082 if (d == nullptr) {
1083 if (errno != ENOENT) {
1084 PLOG(WARNING) << "Failed to open " << path;
1085 }
1086 return;
1087 }
1088 dfd = dirfd(d);
1089 while ((de = readdir(d))) {
1090 if (de->d_type == DT_DIR) {
1091 const char *name = de->d_name;
1092 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) {
1093 continue;
1094 }
1095 if (!strcmp(name, ".") || !strcmp(name, "..")) {
1096 continue;
1097 } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) {
1098 continue;
1099 } else {
1100 collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats);
1101 }
1102 }
1103 }
1104 closedir(d);
1105}
1106
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001107static void collectManualExternalStatsForUser(const std::string& path, struct stats* stats) {
1108 FTS *fts;
1109 FTSENT *p;
1110 char *argv[] = { (char*) path.c_str(), nullptr };
1111 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) {
1112 PLOG(ERROR) << "Failed to fts_open " << path;
1113 return;
1114 }
1115 while ((p = fts_read(fts)) != NULL) {
Jeff Sharkey76ddaeb2017-01-25 22:15:42 -07001116 p->fts_number = p->fts_parent->fts_number;
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001117 switch (p->fts_info) {
1118 case FTS_D:
1119 if (p->fts_level == 4
1120 && !strcmp(p->fts_name, "cache")
1121 && !strcmp(p->fts_parent->fts_parent->fts_name, "data")
1122 && !strcmp(p->fts_parent->fts_parent->fts_parent->fts_name, "Android")) {
1123 p->fts_number = 1;
1124 }
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001125 // Fall through to count the directory
1126 case FTS_DEFAULT:
1127 case FTS_F:
1128 case FTS_SL:
1129 case FTS_SLNONE:
1130 int64_t size = (p->fts_statp->st_blocks * 512);
1131 if (p->fts_number == 1) {
1132 stats->cacheSize += size;
1133 }
1134 stats->dataSize += size;
1135 break;
1136 }
1137 }
1138 fts_close(fts);
1139}
1140
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001141binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001142 const std::vector<std::string>& packageNames, int32_t userId, int32_t flags,
1143 int32_t appId, const std::vector<int64_t>& ceDataInodes,
1144 const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) {
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001145 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001146 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001147 for (auto packageName : packageNames) {
1148 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
1149 }
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001150 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001151
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001152 // When modifying this logic, always verify using tests:
1153 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize
1154
1155#if MEASURE_DEBUG
1156 LOG(INFO) << "Measuring user " << userId << " app " << appId;
1157#endif
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001158
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001159 // Here's a summary of the common storage locations across the platform,
1160 // and how they're each tagged:
1161 //
1162 // /data/app/com.example UID system
1163 // /data/app/com.example/oat UID system
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001164 // /data/user/0/com.example UID u0_a10 GID u0_a10
1165 // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache
1166 // /data/media/0/foo.txt UID u0_media_rw
1167 // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image
1168 // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext
1169 // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache
1170 // /data/media/obb/com.example UID system
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001171
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001172 struct stats stats;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001173 struct stats extStats;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001174 memset(&stats, 0, sizeof(stats));
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001175 memset(&extStats, 0, sizeof(extStats));
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001176
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001177 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001178
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001179 auto device = findQuotaDeviceForUuid(uuid);
1180 if (device.empty()) {
1181 flags &= ~FLAG_USE_QUOTA;
1182 }
1183
Jeff Sharkey466459b2017-01-17 15:25:01 -07001184 ATRACE_BEGIN("obb");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001185 for (auto packageName : packageNames) {
1186 auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str());
1187 calculate_tree_size(obbCodePath, &extStats.codeSize);
1188 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001189 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001190
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001191 if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001192 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001193 for (auto codePath : codePaths) {
1194 calculate_tree_size(codePath, &stats.codeSize, -1,
1195 multiuser_get_shared_gid(userId, appId));
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001196 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001197 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001198
Jeff Sharkey466459b2017-01-17 15:25:01 -07001199 ATRACE_BEGIN("quota");
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001200 collectQuotaStats(device, userId, appId, &stats, &extStats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001201 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001202
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001203 } else {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001204 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001205 for (auto codePath : codePaths) {
1206 calculate_tree_size(codePath, &stats.codeSize);
1207 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001208 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001209
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001210 for (size_t i = 0; i < packageNames.size(); i++) {
1211 const char* pkgname = packageNames[i].c_str();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001212
Jeff Sharkey466459b2017-01-17 15:25:01 -07001213 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001214 auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]);
1215 collectManualStats(cePath, &stats);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001216 auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname);
1217 collectManualStats(dePath, &stats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001218 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001219
Jeff Sharkey466459b2017-01-17 15:25:01 -07001220 ATRACE_BEGIN("profiles");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001221 auto userProfilePath = create_data_user_profile_package_path(userId, pkgname);
1222 calculate_tree_size(userProfilePath, &stats.dataSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001223 auto refProfilePath = create_data_ref_profile_package_path(pkgname);
1224 calculate_tree_size(refProfilePath, &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001225 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001226
Jeff Sharkey466459b2017-01-17 15:25:01 -07001227 ATRACE_BEGIN("external");
Jeff Sharkey76ddaeb2017-01-25 22:15:42 -07001228 auto extPath = create_data_media_package_path(uuid_, userId, "data", pkgname);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001229 collectManualStats(extPath, &extStats);
Jeff Sharkey76ddaeb2017-01-25 22:15:42 -07001230 auto mediaPath = create_data_media_package_path(uuid_, userId, "media", pkgname);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001231 calculate_tree_size(mediaPath, &extStats.dataSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001232 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001233 }
1234
Jeff Sharkey466459b2017-01-17 15:25:01 -07001235 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001236 int32_t sharedGid = multiuser_get_shared_gid(userId, appId);
1237 if (sharedGid != -1) {
1238 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize,
1239 sharedGid, -1);
1240 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001241 calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize,
1242 multiuser_get_uid(userId, appId), -1);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001243 ATRACE_END();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001244 }
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001245
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001246 std::vector<int64_t> ret;
1247 ret.push_back(stats.codeSize);
1248 ret.push_back(stats.dataSize);
1249 ret.push_back(stats.cacheSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001250 ret.push_back(extStats.codeSize);
1251 ret.push_back(extStats.dataSize);
1252 ret.push_back(extStats.cacheSize);
1253#if MEASURE_DEBUG
1254 LOG(DEBUG) << "Final result " << toString(ret);
1255#endif
1256 *_aidl_return = ret;
1257 return ok();
1258}
1259
1260binder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid,
1261 int32_t userId, int32_t flags, const std::vector<int32_t>& appIds,
1262 std::vector<int64_t>* _aidl_return) {
1263 ENFORCE_UID(AID_SYSTEM);
1264 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001265 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001266
1267 // When modifying this logic, always verify using tests:
1268 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize
1269
1270#if MEASURE_DEBUG
1271 LOG(INFO) << "Measuring user " << userId;
1272#endif
1273
1274 struct stats stats;
1275 struct stats extStats;
1276 memset(&stats, 0, sizeof(stats));
1277 memset(&extStats, 0, sizeof(extStats));
1278
1279 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1280
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001281 auto device = findQuotaDeviceForUuid(uuid);
1282 if (device.empty()) {
1283 flags &= ~FLAG_USE_QUOTA;
1284 }
1285
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001286 if (flags & FLAG_USE_QUOTA) {
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001287 struct dqblk dq;
1288
1289 ATRACE_BEGIN("obb");
1290 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), AID_MEDIA_OBB,
1291 reinterpret_cast<char*>(&dq)) != 0) {
1292 if (errno != ESRCH) {
1293 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << AID_MEDIA_OBB;
1294 }
1295 } else {
1296#if MEASURE_DEBUG
1297 LOG(DEBUG) << "quotactl() for GID " << AID_MEDIA_OBB << " " << dq.dqb_curspace;
1298#endif
1299 extStats.codeSize += dq.dqb_curspace;
1300 }
1301 ATRACE_END();
1302
Jeff Sharkey466459b2017-01-17 15:25:01 -07001303 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001304 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001305 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001306
Jeff Sharkey466459b2017-01-17 15:25:01 -07001307 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001308 auto cePath = create_data_user_ce_path(uuid_, userId);
1309 collectManualStatsForUser(cePath, &stats, true);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001310 auto dePath = create_data_user_de_path(uuid_, userId);
1311 collectManualStatsForUser(dePath, &stats, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001312 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001313
Jeff Sharkey466459b2017-01-17 15:25:01 -07001314 ATRACE_BEGIN("profile");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001315 auto userProfilePath = create_data_user_profile_path(userId);
1316 calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001317 auto refProfilePath = create_data_ref_profile_path();
1318 calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001319 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001320
Jeff Sharkey466459b2017-01-17 15:25:01 -07001321 ATRACE_BEGIN("external");
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001322 uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW);
1323 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
1324 reinterpret_cast<char*>(&dq)) != 0) {
1325 if (errno != ESRCH) {
1326 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
1327 }
1328 } else {
1329#if MEASURE_DEBUG
1330 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001331#endif
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001332 extStats.dataSize += dq.dqb_curspace;
1333 }
1334 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001335
Jeff Sharkey466459b2017-01-17 15:25:01 -07001336 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001337 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize,
1338 -1, -1, true);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001339 calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize,
1340 -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001341 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001342
Jeff Sharkey466459b2017-01-17 15:25:01 -07001343 ATRACE_BEGIN("quota");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001344 for (auto appId : appIds) {
1345 if (appId >= AID_APP_START) {
1346 collectQuotaStats(device, userId, appId, &stats, &extStats);
1347#if MEASURE_DEBUG
1348 // Sleep to make sure we don't lose logs
1349 usleep(1);
1350#endif
1351 }
1352 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001353 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001354 } else {
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001355 ATRACE_BEGIN("obb");
1356 auto obbPath = create_data_path(uuid_) + "/media/obb";
1357 calculate_tree_size(obbPath, &extStats.codeSize);
1358 ATRACE_END();
1359
Jeff Sharkey466459b2017-01-17 15:25:01 -07001360 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001361 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001362 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001363
Jeff Sharkey466459b2017-01-17 15:25:01 -07001364 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001365 auto cePath = create_data_user_ce_path(uuid_, userId);
1366 collectManualStatsForUser(cePath, &stats);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001367 auto dePath = create_data_user_de_path(uuid_, userId);
1368 collectManualStatsForUser(dePath, &stats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001369 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001370
Jeff Sharkey466459b2017-01-17 15:25:01 -07001371 ATRACE_BEGIN("profile");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001372 auto userProfilePath = create_data_user_profile_path(userId);
1373 calculate_tree_size(userProfilePath, &stats.dataSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001374 auto refProfilePath = create_data_ref_profile_path();
1375 calculate_tree_size(refProfilePath, &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001376 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001377
Jeff Sharkey466459b2017-01-17 15:25:01 -07001378 ATRACE_BEGIN("external");
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001379 auto dataMediaPath = create_data_media_path(uuid_, userId);
1380 collectManualExternalStatsForUser(dataMediaPath, &extStats);
1381#if MEASURE_DEBUG
1382 LOG(DEBUG) << "Measured external data " << extStats.dataSize << " cache "
1383 << extStats.cacheSize;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001384#endif
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001385 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001386
Jeff Sharkey466459b2017-01-17 15:25:01 -07001387 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001388 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001389 calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001390 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001391 }
1392
1393 std::vector<int64_t> ret;
1394 ret.push_back(stats.codeSize);
1395 ret.push_back(stats.dataSize);
1396 ret.push_back(stats.cacheSize);
1397 ret.push_back(extStats.codeSize);
1398 ret.push_back(extStats.dataSize);
1399 ret.push_back(extStats.cacheSize);
1400#if MEASURE_DEBUG
1401 LOG(DEBUG) << "Final result " << toString(ret);
1402#endif
1403 *_aidl_return = ret;
1404 return ok();
1405}
1406
1407binder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid,
1408 int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) {
1409 ENFORCE_UID(AID_SYSTEM);
1410 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001411 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001412
1413 // When modifying this logic, always verify using tests:
1414 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize
1415
1416#if MEASURE_DEBUG
1417 LOG(INFO) << "Measuring external " << userId;
1418#endif
1419
1420 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1421
1422 int64_t totalSize = 0;
1423 int64_t audioSize = 0;
1424 int64_t videoSize = 0;
1425 int64_t imageSize = 0;
1426
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001427 auto device = findQuotaDeviceForUuid(uuid);
1428 if (device.empty()) {
1429 flags &= ~FLAG_USE_QUOTA;
1430 }
1431
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001432 if (flags & FLAG_USE_QUOTA) {
1433 struct dqblk dq;
1434
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001435 uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW);
1436 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
1437 reinterpret_cast<char*>(&dq)) != 0) {
1438 if (errno != ESRCH) {
1439 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
1440 }
1441 } else {
1442#if MEASURE_DEBUG
1443 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
1444#endif
1445 totalSize = dq.dqb_curspace;
1446 }
1447
1448 gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO);
1449 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid,
1450 reinterpret_cast<char*>(&dq)) == 0) {
1451#if MEASURE_DEBUG
1452 LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace;
1453#endif
1454 audioSize = dq.dqb_curspace;
1455 }
1456 gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO);
1457 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid,
1458 reinterpret_cast<char*>(&dq)) == 0) {
1459#if MEASURE_DEBUG
1460 LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace;
1461#endif
1462 videoSize = dq.dqb_curspace;
1463 }
1464 gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE);
1465 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid,
1466 reinterpret_cast<char*>(&dq)) == 0) {
1467#if MEASURE_DEBUG
1468 LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace;
1469#endif
1470 imageSize = dq.dqb_curspace;
1471 }
1472 } else {
1473 FTS *fts;
1474 FTSENT *p;
1475 auto path = create_data_media_path(uuid_, userId);
1476 char *argv[] = { (char*) path.c_str(), nullptr };
1477 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) {
1478 return error("Failed to fts_open " + path);
1479 }
1480 while ((p = fts_read(fts)) != NULL) {
1481 char* ext;
1482 int64_t size = (p->fts_statp->st_blocks * 512);
1483 switch (p->fts_info) {
1484 case FTS_F:
1485 // Only categorize files not belonging to apps
1486 if (p->fts_statp->st_gid < AID_APP_START) {
1487 ext = strrchr(p->fts_name, '.');
1488 if (ext != nullptr) {
1489 switch (MatchExtension(++ext)) {
1490 case AID_MEDIA_AUDIO: audioSize += size; break;
1491 case AID_MEDIA_VIDEO: videoSize += size; break;
1492 case AID_MEDIA_IMAGE: imageSize += size; break;
1493 }
1494 }
1495 }
1496 // Fall through to always count against total
1497 case FTS_D:
1498 case FTS_DEFAULT:
1499 case FTS_SL:
1500 case FTS_SLNONE:
1501 totalSize += size;
1502 break;
1503 }
1504 }
1505 fts_close(fts);
1506 }
1507
1508 std::vector<int64_t> ret;
1509 ret.push_back(totalSize);
1510 ret.push_back(audioSize);
1511 ret.push_back(videoSize);
1512 ret.push_back(imageSize);
1513#if MEASURE_DEBUG
1514 LOG(DEBUG) << "Final result " << toString(ret);
1515#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001516 *_aidl_return = ret;
Jeff Sharkey423e7462016-12-09 18:18:43 -07001517 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001518}
1519
Jeff Sharkey88ddd942017-01-17 18:05:54 -07001520binder::Status InstalldNativeService::setAppQuota(const std::unique_ptr<std::string>& uuid,
1521 int32_t userId, int32_t appId, int64_t cacheQuota) {
1522 ENFORCE_UID(AID_SYSTEM);
1523 CHECK_ARGUMENT_UUID(uuid);
1524 std::lock_guard<std::recursive_mutex> lock(mLock);
1525
1526 int32_t uid = multiuser_get_uid(userId, appId);
1527 mCacheQuotas[uid] = cacheQuota;
1528
1529 return ok();
1530}
1531
David Sehr6727c2d2016-05-17 16:06:22 -07001532// Dumps the contents of a profile file, using pkgname's dex files for pretty
1533// printing the result.
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001534binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName,
1535 const std::string& codePaths, bool* _aidl_return) {
1536 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001537 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001538 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001539
1540 const char* pkgname = packageName.c_str();
Jeff Sharkey90aff262016-12-12 14:28:24 -07001541 const char* code_paths = codePaths.c_str();
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001542
Jeff Sharkey90aff262016-12-12 14:28:24 -07001543 *_aidl_return = dump_profiles(uid, pkgname, code_paths);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001544 return ok();
David Sehr6727c2d2016-05-17 16:06:22 -07001545}
1546
Andreas Gampe4d0f8252016-03-20 11:30:28 -07001547// TODO: Consider returning error codes.
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001548binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName,
Jeff Sharkey423e7462016-12-09 18:18:43 -07001549 bool* _aidl_return) {
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001550 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001551 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001552 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001553
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001554 const char* pkgname = packageName.c_str();
1555 *_aidl_return = analyse_profiles(uid, pkgname);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001556 return ok();
Andreas Gampe4d0f8252016-03-20 11:30:28 -07001557}
1558
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001559binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid,
1560 const std::unique_ptr<std::string>& packageName, const std::string& instructionSet,
1561 int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags,
1562 const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid,
1563 const std::unique_ptr<std::string>& sharedLibraries) {
1564 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001565 CHECK_ARGUMENT_UUID(uuid);
1566 if (packageName && *packageName != "*") {
1567 CHECK_ARGUMENT_PACKAGE_NAME(*packageName);
1568 }
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001569 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001570
1571 const char* apk_path = apkPath.c_str();
1572 const char* pkgname = packageName ? packageName->c_str() : "*";
1573 const char* instruction_set = instructionSet.c_str();
1574 const char* oat_dir = outputPath ? outputPath->c_str() : nullptr;
1575 const char* compiler_filter = compilerFilter.c_str();
1576 const char* volume_uuid = uuid ? uuid->c_str() : nullptr;
1577 const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr;
1578
1579 int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded,
1580 oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001581 return res ? error(res, "Failed to dexopt") : ok();
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001582}
1583
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001584binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) {
1585 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001586 std::lock_guard<std::recursive_mutex> lock(mLock);
1587
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001588 const char* instruction_set = instructionSet.c_str();
1589
1590 char boot_marker_path[PKG_PATH_MAX];
1591 sprintf(boot_marker_path,
Andreas Gampe02d0de52015-11-11 20:43:16 -08001592 "%s/%s/%s/.booting",
1593 android_data_dir.path,
1594 DALVIK_CACHE,
1595 instruction_set);
Narayan Kamath091ea772014-11-10 15:03:46 +00001596
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001597 ALOGV("mark_boot_complete : %s", boot_marker_path);
1598 if (unlink(boot_marker_path) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001599 return error(StringPrintf("Failed to unlink %s", boot_marker_path));
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001600 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001601 return ok();
Narayan Kamath091ea772014-11-10 15:03:46 +00001602}
1603
Mike Lockwood94afecf2012-10-24 10:45:23 -07001604void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1605 struct stat* statbuf)
1606{
1607 while (path[basepos] != 0) {
1608 if (path[basepos] == '/') {
1609 path[basepos] = 0;
1610 if (lstat(path, statbuf) < 0) {
1611 ALOGV("Making directory: %s\n", path);
1612 if (mkdir(path, mode) == 0) {
1613 chown(path, uid, gid);
1614 } else {
1615 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1616 }
1617 }
1618 path[basepos] = '/';
1619 basepos++;
1620 }
1621 basepos++;
1622 }
1623}
1624
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001625binder::Status InstalldNativeService::linkNativeLibraryDirectory(
1626 const std::unique_ptr<std::string>& uuid, const std::string& packageName,
1627 const std::string& nativeLibPath32, int32_t userId) {
1628 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001629 CHECK_ARGUMENT_UUID(uuid);
1630 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001631 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001632
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001633 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1634 const char* pkgname = packageName.c_str();
1635 const char* asecLibDir = nativeLibPath32.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001636 struct stat s, libStat;
Jeff Sharkey423e7462016-12-09 18:18:43 -07001637 binder::Status res = ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001638
Jeff Sharkey423e7462016-12-09 18:18:43 -07001639 auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname);
1640 auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX;
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001641
1642 const char* pkgdir = _pkgdir.c_str();
1643 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001644
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001645 if (stat(pkgdir, &s) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001646 return error("Failed to stat " + _pkgdir);
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001647 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001648
1649 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001650 return error("Failed to chown " + _pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001651 }
1652
1653 if (chmod(pkgdir, 0700) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001654 res = error("Failed to chmod " + _pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001655 goto out;
1656 }
1657
1658 if (lstat(libsymlink, &libStat) < 0) {
1659 if (errno != ENOENT) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001660 res = error("Failed to stat " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001661 goto out;
1662 }
1663 } else {
1664 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001665 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001666 res = error("Failed to delete " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001667 goto out;
1668 }
1669 } else if (S_ISLNK(libStat.st_mode)) {
1670 if (unlink(libsymlink) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001671 res = error("Failed to unlink " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001672 goto out;
1673 }
1674 }
1675 }
1676
1677 if (symlink(asecLibDir, libsymlink) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001678 res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001679 goto out;
1680 }
1681
1682out:
1683 if (chmod(pkgdir, s.st_mode) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001684 auto msg = "Failed to cleanup chmod " + _pkgdir;
1685 if (res.isOk()) {
1686 res = error(msg);
1687 } else {
1688 PLOG(ERROR) << msg;
1689 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001690 }
1691
1692 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001693 auto msg = "Failed to cleanup chown " + _pkgdir;
1694 if (res.isOk()) {
1695 res = error(msg);
1696 } else {
1697 PLOG(ERROR) << msg;
1698 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001699 }
1700
Jeff Sharkey423e7462016-12-09 18:18:43 -07001701 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001702}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001703
1704static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1705{
1706 static const char *IDMAP_BIN = "/system/bin/idmap";
1707 static const size_t MAX_INT_LEN = 32;
1708 char idmap_str[MAX_INT_LEN];
1709
1710 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1711
1712 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1713 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1714}
1715
1716// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1717// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1718static int flatten_path(const char *prefix, const char *suffix,
1719 const char *overlay_path, char *idmap_path, size_t N)
1720{
1721 if (overlay_path == NULL || idmap_path == NULL) {
1722 return -1;
1723 }
1724 const size_t len_overlay_path = strlen(overlay_path);
1725 // will access overlay_path + 1 further below; requires absolute path
1726 if (len_overlay_path < 2 || *overlay_path != '/') {
1727 return -1;
1728 }
1729 const size_t len_idmap_root = strlen(prefix);
1730 const size_t len_suffix = strlen(suffix);
1731 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1732 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1733 // additions below would cause overflow
1734 return -1;
1735 }
1736 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1737 return -1;
1738 }
1739 memset(idmap_path, 0, N);
1740 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1741 char *ch = idmap_path + len_idmap_root;
1742 while (*ch != '\0') {
1743 if (*ch == '/') {
1744 *ch = '@';
1745 }
1746 ++ch;
1747 }
1748 return 0;
1749}
1750
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001751binder::Status InstalldNativeService::idmap(const std::string& targetApkPath,
1752 const std::string& overlayApkPath, int32_t uid) {
1753 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001754 std::lock_guard<std::recursive_mutex> lock(mLock);
1755
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001756 const char* target_apk = targetApkPath.c_str();
1757 const char* overlay_apk = overlayApkPath.c_str();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001758 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1759
1760 int idmap_fd = -1;
1761 char idmap_path[PATH_MAX];
1762
1763 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1764 idmap_path, sizeof(idmap_path)) == -1) {
1765 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1766 goto fail;
1767 }
1768
1769 unlink(idmap_path);
1770 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1771 if (idmap_fd < 0) {
1772 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1773 goto fail;
1774 }
1775 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1776 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1777 goto fail;
1778 }
1779 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1780 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1781 goto fail;
1782 }
1783
1784 pid_t pid;
1785 pid = fork();
1786 if (pid == 0) {
1787 /* child -- drop privileges before continuing */
1788 if (setgid(uid) != 0) {
1789 ALOGE("setgid(%d) failed during idmap\n", uid);
1790 exit(1);
1791 }
1792 if (setuid(uid) != 0) {
1793 ALOGE("setuid(%d) failed during idmap\n", uid);
1794 exit(1);
1795 }
1796 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1797 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1798 exit(1);
1799 }
1800
1801 run_idmap(target_apk, overlay_apk, idmap_fd);
1802 exit(1); /* only if exec call to idmap failed */
1803 } else {
1804 int status = wait_child(pid);
1805 if (status != 0) {
1806 ALOGE("idmap failed, status=0x%04x\n", status);
1807 goto fail;
1808 }
1809 }
1810
1811 close(idmap_fd);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001812 return ok();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001813fail:
1814 if (idmap_fd >= 0) {
1815 close(idmap_fd);
1816 unlink(idmap_path);
1817 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001818 return error();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001819}
Robert Craige9887e42014-02-20 10:25:56 -05001820
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001821binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid,
1822 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
1823 const std::string& seInfo) {
1824 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001825 CHECK_ARGUMENT_UUID(uuid);
1826 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001827 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001828
1829 binder::Status res = ok();
Robert Craige9887e42014-02-20 10:25:56 -05001830
Robert Craigda30dc72014-03-27 10:21:12 -04001831 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001832 unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE;
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001833 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1834 const char* pkgName = packageName.c_str();
1835 const char* seinfo = seInfo.c_str();
Robert Craigda30dc72014-03-27 10:21:12 -04001836
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001837 uid_t uid = multiuser_get_uid(userId, appId);
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -07001838 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001839 auto path = create_data_user_ce_package_path(uuid_, userId, pkgName);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001840 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001841 res = error("restorecon failed for " + path);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001842 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001843 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -07001844 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001845 auto path = create_data_user_de_package_path(uuid_, userId, pkgName);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001846 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001847 res = error("restorecon failed for " + path);
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001848 }
Robert Craige9887e42014-02-20 10:25:56 -05001849 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001850 return res;
Robert Craige9887e42014-02-20 10:25:56 -05001851}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001852
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001853binder::Status InstalldNativeService::createOatDir(const std::string& oatDir,
1854 const std::string& instructionSet) {
1855 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001856 std::lock_guard<std::recursive_mutex> lock(mLock);
1857
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001858 const char* oat_dir = oatDir.c_str();
1859 const char* instruction_set = instructionSet.c_str();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001860 char oat_instr_dir[PKG_PATH_MAX];
1861
1862 if (validate_apk_path(oat_dir)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001863 return error("Invalid path " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001864 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001865 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001866 return error("Failed to prepare " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001867 }
1868 if (selinux_android_restorecon(oat_dir, 0)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001869 return error("Failed to restorecon " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001870 }
1871 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001872 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001873 return error(StringPrintf("Failed to prepare %s", oat_instr_dir));
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001874 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001875 return ok();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001876}
1877
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001878binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) {
1879 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001880 std::lock_guard<std::recursive_mutex> lock(mLock);
1881
Jeff Sharkey423e7462016-12-09 18:18:43 -07001882 if (validate_apk_path(packageDir.c_str())) {
1883 return error("Invalid path " + packageDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001884 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001885 if (delete_dir_contents_and_dir(packageDir) != 0) {
1886 return error("Failed to delete " + packageDir);
1887 }
1888 return ok();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001889}
1890
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001891binder::Status InstalldNativeService::linkFile(const std::string& relativePath,
1892 const std::string& fromBase, const std::string& toBase) {
1893 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001894 std::lock_guard<std::recursive_mutex> lock(mLock);
1895
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001896 const char* relative_path = relativePath.c_str();
1897 const char* from_base = fromBase.c_str();
1898 const char* to_base = toBase.c_str();
Narayan Kamathd845c962015-06-04 13:20:27 +01001899 char from_path[PKG_PATH_MAX];
1900 char to_path[PKG_PATH_MAX];
1901 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
1902 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
1903
1904 if (validate_apk_path_subdirs(from_path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001905 return error(StringPrintf("Invalid from path %s", from_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001906 }
1907
1908 if (validate_apk_path_subdirs(to_path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001909 return error(StringPrintf("Invalid to path %s", to_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001910 }
1911
Jeff Sharkey423e7462016-12-09 18:18:43 -07001912 if (link(from_path, to_path) < 0) {
1913 return error(StringPrintf("Failed to link from %s to %s", from_path, to_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001914 }
1915
Jeff Sharkey423e7462016-12-09 18:18:43 -07001916 return ok();
Narayan Kamathd845c962015-06-04 13:20:27 +01001917}
1918
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001919binder::Status InstalldNativeService::moveAb(const std::string& apkPath,
1920 const std::string& instructionSet, const std::string& outputPath) {
1921 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001922 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001923
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001924 const char* apk_path = apkPath.c_str();
1925 const char* instruction_set = instructionSet.c_str();
1926 const char* oat_dir = outputPath.c_str();
Andreas Gamped089ca12016-06-27 14:25:30 -07001927
Jeff Sharkey90aff262016-12-12 14:28:24 -07001928 bool success = move_ab(apk_path, instruction_set, oat_dir);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001929 return success ? ok() : error();
Andreas Gampee65b4fa2016-03-01 11:46:45 -08001930}
1931
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001932binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath,
1933 const std::string& instructionSet, const std::string& outputPath) {
1934 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001935 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001936
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001937 const char* apk_path = apkPath.c_str();
1938 const char* instruction_set = instructionSet.c_str();
1939 const char* oat_dir = outputPath.c_str();
1940
Jeff Sharkey90aff262016-12-12 14:28:24 -07001941 bool res = delete_odex(apk_path, instruction_set, oat_dir);
1942 return res ? ok() : error();
Andreas Gampeb31206b2016-09-09 17:07:04 -07001943}
1944
Calin Juravlec9eab382017-01-25 01:17:17 -08001945binder::Status InstalldNativeService::reconcileSecondaryDexFile(
1946 const std::string& dexPath, const std::string& packageName, int32_t uid,
1947 const std::vector<std::string>& isas, const std::unique_ptr<std::string>& volumeUuid,
1948 int32_t storage_flag, bool* _aidl_return) {
1949 ENFORCE_UID(AID_SYSTEM);
1950 CHECK_ARGUMENT_UUID(volumeUuid);
1951 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
1952
1953 std::lock_guard<std::recursive_mutex> lock(mLock);
1954 bool result = android::installd::reconcile_secondary_dex_file(
1955 dexPath, packageName, uid, isas, volumeUuid, storage_flag, _aidl_return);
1956 return result ? ok() : error();
1957}
1958
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001959binder::Status InstalldNativeService::invalidateMounts() {
1960 ENFORCE_UID(AID_SYSTEM);
1961 std::lock_guard<std::recursive_mutex> lock(mLock);
1962
1963 mQuotaDevices.clear();
1964
1965 std::ifstream in("/proc/mounts");
1966 if (!in.is_open()) {
1967 return error("Failed to read mounts");
1968 }
1969
1970 std::string source;
1971 std::string target;
1972 std::string ignored;
1973 struct dqblk dq;
1974 while (!in.eof()) {
1975 std::getline(in, source, ' ');
1976 std::getline(in, target, ' ');
1977 std::getline(in, ignored);
1978
1979 if (source.compare(0, 11, "/dev/block/") == 0) {
1980 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0,
1981 reinterpret_cast<char*>(&dq)) == 0) {
1982 LOG(DEBUG) << "Found " << source << " with quota";
1983 mQuotaDevices[target] = source;
1984 }
1985 }
1986 }
1987 return ok();
1988}
1989
1990std::string InstalldNativeService::findQuotaDeviceForUuid(
1991 const std::unique_ptr<std::string>& uuid) {
1992 auto path = create_data_path(uuid ? uuid->c_str() : nullptr);
1993 return mQuotaDevices[path];
1994}
1995
Andreas Gampe02d0de52015-11-11 20:43:16 -08001996} // namespace installd
1997} // namespace android