blob: 381e90c117ee84a8b7e9ce389bc7f3e5147b981f [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 Gampe0354bd02016-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 Gampeafa58d12016-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
Janis Danisevskis40bd3ef2016-06-07 10:31:27 -070075static constexpr const int MIN_RESTRICTED_HOME_SDK_VERSION = 24; // > M
Janis Danisevskiseecb2d22016-01-12 14:45:55 +000076
Andreas Gampe0354bd02016-06-27 14:25:30 -070077static constexpr const char* PKG_LIB_POSTFIX = "/lib";
78static constexpr const char* CACHE_DIR_POSTFIX = "/cache";
79static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache";
80
81static constexpr const char* IDMAP_PREFIX = "/data/resource-cache/";
82static constexpr const char* IDMAP_SUFFIX = "@idmap";
83
Andreas Gampe0354bd02016-06-27 14:25:30 -070084// NOTE: keep in sync with Installer
85static constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8;
86static constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070087static constexpr int FLAG_USE_QUOTA = 1 << 12;
Jeff Sharkey88ddd942017-01-17 18:05:54 -070088static constexpr int FLAG_FREE_CACHE_V2 = 1 << 13;
Jeff Sharkey871a8f22017-02-21 18:30:28 -070089static constexpr int FLAG_FREE_CACHE_V2_DEFY_QUOTA = 1 << 14;
90static constexpr int FLAG_FREE_CACHE_NOOP = 1 << 15;
Calin Juravle6a1648e2016-02-01 12:12:16 +000091
Jeff Sharkey0274c972016-12-06 09:32:04 -070092namespace {
93
94constexpr const char* kDump = "android.permission.DUMP";
95
Jeff Sharkey423e7462016-12-09 18:18:43 -070096static binder::Status ok() {
97 return binder::Status::ok();
98}
99
100static binder::Status exception(uint32_t code, const std::string& msg) {
101 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
102}
103
104static binder::Status error() {
105 return binder::Status::fromServiceSpecificError(errno);
106}
107
108static binder::Status error(const std::string& msg) {
109 PLOG(ERROR) << msg;
110 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
111}
112
113static binder::Status error(uint32_t code, const std::string& msg) {
114 LOG(ERROR) << msg << " (" << code << ")";
115 return binder::Status::fromServiceSpecificError(code, String8(msg.c_str()));
116}
117
Jeff Sharkey0274c972016-12-06 09:32:04 -0700118binder::Status checkPermission(const char* permission) {
119 pid_t pid;
120 uid_t uid;
121
122 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
123 reinterpret_cast<int32_t*>(&uid))) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700124 return ok();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700125 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700126 return exception(binder::Status::EX_SECURITY,
127 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
Jeff Sharkey0274c972016-12-06 09:32:04 -0700128 }
129}
130
131binder::Status checkUid(uid_t expectedUid) {
132 uid_t uid = IPCThreadState::self()->getCallingUid();
133 if (uid == expectedUid || uid == AID_ROOT) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700134 return ok();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700135 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700136 return exception(binder::Status::EX_SECURITY,
137 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
138 }
139}
140
141binder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) {
142 if (!uuid || is_valid_filename(*uuid)) {
143 return ok();
144 } else {
145 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
146 StringPrintf("UUID %s is malformed", uuid->c_str()));
147 }
148}
149
150binder::Status checkArgumentPackageName(const std::string& packageName) {
151 if (is_valid_package_name(packageName.c_str())) {
152 return ok();
153 } else {
154 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
155 StringPrintf("Package name %s is malformed", packageName.c_str()));
Jeff Sharkey0274c972016-12-06 09:32:04 -0700156 }
157}
158
159#define ENFORCE_UID(uid) { \
160 binder::Status status = checkUid((uid)); \
161 if (!status.isOk()) { \
162 return status; \
163 } \
164}
165
Jeff Sharkey423e7462016-12-09 18:18:43 -0700166#define CHECK_ARGUMENT_UUID(uuid) { \
167 binder::Status status = checkArgumentUuid((uuid)); \
168 if (!status.isOk()) { \
169 return status; \
170 } \
171}
172
173#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \
174 binder::Status status = \
175 checkArgumentPackageName((packageName)); \
176 if (!status.isOk()) { \
177 return status; \
178 } \
179}
180
Jeff Sharkey0274c972016-12-06 09:32:04 -0700181} // namespace
182
183status_t InstalldNativeService::start() {
184 IPCThreadState::self()->disableBackgroundScheduling(true);
185 status_t ret = BinderService<InstalldNativeService>::publish();
186 if (ret != android::OK) {
187 return ret;
188 }
189 sp<ProcessState> ps(ProcessState::self());
190 ps->startThreadPool();
191 ps->giveThreadPoolName();
192 return android::OK;
193}
194
195status_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) {
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700196 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
Jeff Sharkey0274c972016-12-06 09:32:04 -0700197 const binder::Status dump_permission = checkPermission(kDump);
198 if (!dump_permission.isOk()) {
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700199 out << dump_permission.toString8() << endl;
Jeff Sharkey0274c972016-12-06 09:32:04 -0700200 return PERMISSION_DENIED;
201 }
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700202 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700203
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700204 out << "installd is happy!" << endl;
205
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700206 {
207 std::lock_guard<std::recursive_mutex> lock(mQuotaDevicesLock);
208 out << endl << "Devices with quota support:" << endl;
209 for (const auto& n : mQuotaDevices) {
210 out << " " << n.first << " = " << n.second << endl;
211 }
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700212 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700213
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700214 {
215 std::lock_guard<std::recursive_mutex> lock(mCacheQuotasLock);
216 out << endl << "Per-UID cache quotas:" << endl;
217 for (const auto& n : mCacheQuotas) {
218 out << " " << n.first << " = " << n.second << endl;
219 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700220 }
221
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700222 out << endl;
223 out.flush();
224
Jeff Sharkey0274c972016-12-06 09:32:04 -0700225 return NO_ERROR;
226}
227
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600228/**
229 * Perform restorecon of the given path, but only perform recursive restorecon
230 * if the label of that top-level file actually changed. This can save us
231 * significant time by avoiding no-op traversals of large filesystem trees.
232 */
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700233static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid,
234 bool existing) {
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600235 int res = 0;
236 char* before = nullptr;
237 char* after = nullptr;
238
239 // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by
240 // libselinux. Not needed here.
241
Jeff Sharkey8567ebf2016-10-31 11:22:19 -0600242 if (lgetfilecon(path.c_str(), &before) < 0) {
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600243 PLOG(ERROR) << "Failed before getfilecon for " << path;
244 goto fail;
245 }
Jeff Sharkey0274c972016-12-06 09:32:04 -0700246 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) {
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600247 PLOG(ERROR) << "Failed top-level restorecon for " << path;
248 goto fail;
249 }
Jeff Sharkey8567ebf2016-10-31 11:22:19 -0600250 if (lgetfilecon(path.c_str(), &after) < 0) {
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600251 PLOG(ERROR) << "Failed after getfilecon for " << path;
252 goto fail;
253 }
254
255 // If the initial top-level restorecon above changed the label, then go
256 // back and restorecon everything recursively
257 if (strcmp(before, after)) {
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700258 if (existing) {
259 LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at "
260 << path << "; running recursive restorecon";
261 }
Jeff Sharkey0274c972016-12-06 09:32:04 -0700262 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid,
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600263 SELINUX_ANDROID_RESTORECON_RECURSE) < 0) {
264 PLOG(ERROR) << "Failed recursive restorecon for " << path;
265 goto fail;
266 }
267 }
268
269 goto done;
270fail:
271 res = -1;
272done:
273 free(before);
274 free(after);
275 return res;
276}
277
Jeff Sharkey0274c972016-12-06 09:32:04 -0700278static int restorecon_app_data_lazy(const std::string& parent, const char* name,
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700279 const std::string& seInfo, uid_t uid, bool existing) {
280 return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid,
281 existing);
Jeff Sharkey8567ebf2016-10-31 11:22:19 -0600282}
283
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600284static int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) {
Jeff Sharkey1f6a7f12016-07-14 18:16:22 -0600285 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) {
286 PLOG(ERROR) << "Failed to prepare " << path;
287 return -1;
288 }
Jeff Sharkey1f6a7f12016-07-14 18:16:22 -0600289 return 0;
290}
291
Jeff Sharkey0274c972016-12-06 09:32:04 -0700292binder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid,
293 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700294 const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700295 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700296 CHECK_ARGUMENT_UUID(uuid);
297 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700298 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700299
300 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
301 const char* pkgname = packageName.c_str();
302
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700303 // Assume invalid inode unless filled in below
304 if (_aidl_return != nullptr) *_aidl_return = -1;
305
Jeff Sharkey24ef15b2017-01-12 19:27:03 -0700306 int32_t uid = multiuser_get_uid(userId, appId);
307 int32_t cacheGid = multiuser_get_cache_gid(userId, appId);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700308 mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751;
309
Jeff Sharkey24ef15b2017-01-12 19:27:03 -0700310 // If UID doesn't have a specific cache GID, use UID value
311 if (cacheGid == -1) {
312 cacheGid = uid;
313 }
314
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700315 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700316 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname);
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700317 bool existing = (access(path.c_str(), F_OK) == 0);
318
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700319 if (prepare_app_dir(path, targetMode, uid) ||
320 prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) ||
321 prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700322 return error("Failed to prepare " + path);
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600323 }
324
325 // Consider restorecon over contents if label changed
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700326 if (restorecon_app_data_lazy(path, seInfo, uid, existing) ||
327 restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) ||
328 restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700329 return error("Failed to restorecon " + path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700330 }
Jeff Sharkey1f6a7f12016-07-14 18:16:22 -0600331
332 // Remember inode numbers of cache directories so that we can clear
333 // contents while CE storage is locked
334 if (write_path_inode(path, "cache", kXattrInodeCache) ||
335 write_path_inode(path, "code_cache", kXattrInodeCodeCache)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700336 return error("Failed to write_path_inode for " + path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700337 }
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700338
339 // And return the CE inode of the top-level data directory so we can
340 // clear contents while CE storage is locked
341 if ((_aidl_return != nullptr)
342 && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) {
343 return error("Failed to get_path_inode for " + path);
344 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700345 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700346 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700347 auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700348 bool existing = (access(path.c_str(), F_OK) == 0);
349
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700350 if (prepare_app_dir(path, targetMode, uid) ||
351 prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) ||
352 prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700353 return error("Failed to prepare " + path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700354 }
Calin Juravle6a1648e2016-02-01 12:12:16 +0000355
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600356 // Consider restorecon over contents if label changed
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700357 if (restorecon_app_data_lazy(path, seInfo, uid, existing)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700358 return error("Failed to restorecon " + path);
Jeff Sharkey22f6fd52016-09-20 18:21:42 -0600359 }
360
Jeff Sharkey90aff262016-12-12 14:28:24 -0700361 if (property_get_bool("dalvik.vm.usejitprofiles", false)) {
Calin Juravle114f0812017-03-08 19:05:07 -0800362 const std::string profile_dir =
363 create_primary_current_profile_package_dir_path(userId, pkgname);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000364 // read-write-execute only for the app user.
Calin Juravle114f0812017-03-08 19:05:07 -0800365 if (fs_prepare_dir_strict(profile_dir.c_str(), 0700, uid, uid) != 0) {
366 return error("Failed to prepare " + profile_dir);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000367 }
Calin Juravle114f0812017-03-08 19:05:07 -0800368 const std::string profile_file = create_current_profile_path(userId, pkgname,
369 /*is_secondary_dex*/false);
Calin Juravlebc56e7d2016-05-24 15:33:12 +0100370 // read-write only for the app user.
371 if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) {
Calin Juravle114f0812017-03-08 19:05:07 -0800372 return error("Failed to prepare " + profile_file);
Calin Juravlebc56e7d2016-05-24 15:33:12 +0100373 }
Calin Juravle114f0812017-03-08 19:05:07 -0800374 const std::string ref_profile_path =
375 create_primary_reference_profile_package_dir_path(pkgname);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000376 // dex2oat/profman runs under the shared app gid and it needs to read/write reference
377 // profiles.
Jeff Sharkeyadddd982017-01-03 14:33:50 -0700378 int shared_app_gid = multiuser_get_shared_gid(0, appId);
Jeff Sharkey2e6dc9b2016-12-15 14:50:11 -0700379 if ((shared_app_gid != -1) && fs_prepare_dir_strict(
Calin Juravle6a1648e2016-02-01 12:12:16 +0000380 ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700381 return error("Failed to prepare " + ref_profile_path);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000382 }
383 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700384 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700385 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700386}
387
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700388binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid,
389 const std::string& packageName, int32_t userId, int32_t flags) {
390 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700391 CHECK_ARGUMENT_UUID(uuid);
392 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700393 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700394
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700395 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
396 const char* pkgname = packageName.c_str();
397
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700398 // This method only exists to upgrade system apps that have requested
399 // forceDeviceEncrypted, so their default storage always lives in a
400 // consistent location. This only works on non-FBE devices, since we
401 // never want to risk exposing data on a device with real CE/DE storage.
402
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700403 auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname);
404 auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700405
406 // If neither directory is marked as default, assume CE is default
407 if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1
408 && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) {
409 if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700410 return error("Failed to mark default storage " + ce_path);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700411 }
412 }
413
414 // Migrate default data location if needed
415 auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path;
416 auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path;
417
418 if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) {
419 LOG(WARNING) << "Requested default storage " << target
420 << " is not active; migrating from " << source;
421 if (delete_dir_contents_and_dir(target) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700422 return error("Failed to delete " + target);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700423 }
424 if (rename(source.c_str(), target.c_str()) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700425 return error("Failed to rename " + source + " to " + target);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700426 }
427 }
428
Jeff Sharkey423e7462016-12-09 18:18:43 -0700429 return ok();
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700430}
431
Calin Juravle6a1648e2016-02-01 12:12:16 +0000432
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700433binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) {
434 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700435 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700436 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700437
Jeff Sharkey423e7462016-12-09 18:18:43 -0700438 binder::Status res = ok();
Calin Juravle114f0812017-03-08 19:05:07 -0800439 if (!clear_primary_reference_profile(packageName)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700440 res = error("Failed to clear reference profile for " + packageName);
441 }
Calin Juravle114f0812017-03-08 19:05:07 -0800442 if (!clear_primary_current_profiles(packageName)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700443 res = error("Failed to clear current profiles for " + packageName);
444 }
445 return res;
Calin Juravle6a1648e2016-02-01 12:12:16 +0000446}
447
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700448binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid,
449 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
450 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700451 CHECK_ARGUMENT_UUID(uuid);
452 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700453 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700454
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700455 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
456 const char* pkgname = packageName.c_str();
457
Jeff Sharkey423e7462016-12-09 18:18:43 -0700458 binder::Status res = ok();
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700459 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700460 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
Jeff Sharkey1f6a7f12016-07-14 18:16:22 -0600461 if (flags & FLAG_CLEAR_CACHE_ONLY) {
462 path = read_path_inode(path, "cache", kXattrInodeCache);
463 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
464 path = read_path_inode(path, "code_cache", kXattrInodeCodeCache);
465 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700466 if (access(path.c_str(), F_OK) == 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700467 if (delete_dir_contents(path) != 0) {
468 res = error("Failed to delete contents of " + path);
469 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700470 }
471 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700472 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey1f6a7f12016-07-14 18:16:22 -0600473 std::string suffix = "";
474 bool only_cache = false;
475 if (flags & FLAG_CLEAR_CACHE_ONLY) {
476 suffix = CACHE_DIR_POSTFIX;
477 only_cache = true;
478 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
479 suffix = CODE_CACHE_DIR_POSTFIX;
480 only_cache = true;
481 }
482
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700483 auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix;
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700484 if (access(path.c_str(), F_OK) == 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700485 if (delete_dir_contents(path) != 0) {
486 res = error("Failed to delete contents of " + path);
487 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700488 }
Calin Juravleedae6692016-03-23 13:55:31 +0000489 if (!only_cache) {
Calin Juravle114f0812017-03-08 19:05:07 -0800490 if (!clear_primary_current_profile(packageName, userId)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700491 res = error("Failed to clear current profile for " + packageName);
Calin Juravleedae6692016-03-23 13:55:31 +0000492 }
493 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700494 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700495 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700496}
497
Calin Juravle76268c52017-03-09 13:19:42 -0800498static int destroy_app_reference_profile(const std::string& pkgname) {
Calin Juravle7535e8e2016-03-29 16:05:40 +0100499 return delete_dir_contents_and_dir(
Calin Juravle114f0812017-03-08 19:05:07 -0800500 create_primary_reference_profile_package_dir_path(pkgname),
Calin Juravle7535e8e2016-03-29 16:05:40 +0100501 /*ignore_if_missing*/ true);
502}
503
Calin Juravle76268c52017-03-09 13:19:42 -0800504static int destroy_app_current_profiles(const std::string& pkgname, userid_t userid) {
Calin Juravleb06f98a2016-03-28 15:11:01 +0100505 return delete_dir_contents_and_dir(
Calin Juravle114f0812017-03-08 19:05:07 -0800506 create_primary_current_profile_package_dir_path(userid, pkgname),
Calin Juravleb06f98a2016-03-28 15:11:01 +0100507 /*ignore_if_missing*/ true);
Calin Juravleedae6692016-03-23 13:55:31 +0000508}
509
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700510binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) {
511 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700512 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700513 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700514
Jeff Sharkey423e7462016-12-09 18:18:43 -0700515 binder::Status res = ok();
Calin Juravleedae6692016-03-23 13:55:31 +0000516 std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
517 for (auto user : users) {
Calin Juravle76268c52017-03-09 13:19:42 -0800518 if (destroy_app_current_profiles(packageName, user) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700519 res = error("Failed to destroy current profiles for " + packageName);
520 }
Calin Juravleedae6692016-03-23 13:55:31 +0000521 }
Calin Juravle76268c52017-03-09 13:19:42 -0800522 if (destroy_app_reference_profile(packageName) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700523 res = error("Failed to destroy reference profile for " + packageName);
524 }
525 return res;
Calin Juravlecaa6b802016-03-22 14:15:52 +0000526}
527
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700528binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid,
529 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
530 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700531 CHECK_ARGUMENT_UUID(uuid);
532 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700533 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700534
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700535 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
536 const char* pkgname = packageName.c_str();
537
Jeff Sharkey423e7462016-12-09 18:18:43 -0700538 binder::Status res = ok();
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700539 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700540 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
541 if (delete_dir_contents_and_dir(path) != 0) {
542 res = error("Failed to delete " + path);
543 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700544 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700545 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700546 auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
547 if (delete_dir_contents_and_dir(path) != 0) {
548 res = error("Failed to delete " + path);
549 }
Calin Juravle76268c52017-03-09 13:19:42 -0800550 destroy_app_current_profiles(packageName, userId);
Calin Juravle7535e8e2016-03-29 16:05:40 +0100551 // TODO(calin): If the package is still installed by other users it's probably
552 // beneficial to keep the reference profile around.
553 // Verify if it's ok to do that.
Calin Juravle76268c52017-03-09 13:19:42 -0800554 destroy_app_reference_profile(packageName);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700555 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700556 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700557}
558
Jeff Sharkey0274c972016-12-06 09:32:04 -0700559binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid,
560 const std::unique_ptr<std::string>& toUuid, const std::string& packageName,
561 const std::string& dataAppName, int32_t appId, const std::string& seInfo,
562 int32_t targetSdkVersion) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700563 ENFORCE_UID(AID_SYSTEM);
564 CHECK_ARGUMENT_UUID(fromUuid);
565 CHECK_ARGUMENT_UUID(toUuid);
566 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700567 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700568
569 const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr;
570 const char* to_uuid = toUuid ? toUuid->c_str() : nullptr;
571 const char* package_name = packageName.c_str();
572 const char* data_app_name = dataAppName.c_str();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700573
Jeff Sharkey423e7462016-12-09 18:18:43 -0700574 binder::Status res = ok();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700575 std::vector<userid_t> users = get_known_users(from_uuid);
576
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700577 // Copy app
578 {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600579 auto from = create_data_app_package_path(from_uuid, data_app_name);
580 auto to = create_data_app_package_path(to_uuid, data_app_name);
581 auto to_parent = create_data_app_path(to_uuid);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700582
583 char *argv[] = {
584 (char*) kCpPath,
585 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
586 (char*) "-p", /* preserve timestamps, ownership, and permissions */
587 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
588 (char*) "-P", /* Do not follow symlinks [default] */
589 (char*) "-d", /* don't dereference symlinks */
590 (char*) from.c_str(),
591 (char*) to_parent.c_str()
592 };
593
594 LOG(DEBUG) << "Copying " << from << " to " << to;
595 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700596 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700597 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700598 goto fail;
599 }
600
601 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700602 res = error("Failed to restorecon " + to);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700603 goto fail;
604 }
605 }
606
607 // Copy private data for all known users
Jeff Sharkeye3637242015-04-08 20:56:42 -0700608 for (auto user : users) {
Jeff Sharkeye3637242015-04-08 20:56:42 -0700609
610 // Data source may not exist for all users; that's okay
Jeff Sharkey51c94492016-05-10 17:46:39 -0600611 auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name);
612 if (access(from_ce.c_str(), F_OK) != 0) {
613 LOG(INFO) << "Missing source " << from_ce;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700614 continue;
615 }
616
Jeff Sharkey0274c972016-12-06 09:32:04 -0700617 if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId,
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700618 seInfo, targetSdkVersion, nullptr).isOk()) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700619 res = error("Failed to create package target");
Jeff Sharkeye3637242015-04-08 20:56:42 -0700620 goto fail;
621 }
622
623 char *argv[] = {
624 (char*) kCpPath,
625 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
626 (char*) "-p", /* preserve timestamps, ownership, and permissions */
627 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
628 (char*) "-P", /* Do not follow symlinks [default] */
629 (char*) "-d", /* don't dereference symlinks */
Jeff Sharkey51c94492016-05-10 17:46:39 -0600630 nullptr,
631 nullptr
Jeff Sharkeye3637242015-04-08 20:56:42 -0700632 };
633
Jeff Sharkey51c94492016-05-10 17:46:39 -0600634 {
635 auto from = create_data_user_de_package_path(from_uuid, user, package_name);
636 auto to = create_data_user_de_path(to_uuid, user);
637 argv[6] = (char*) from.c_str();
638 argv[7] = (char*) to.c_str();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700639
Jeff Sharkey51c94492016-05-10 17:46:39 -0600640 LOG(DEBUG) << "Copying " << from << " to " << to;
641 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
642 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700643 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkey51c94492016-05-10 17:46:39 -0600644 goto fail;
645 }
646 }
647 {
648 auto from = create_data_user_ce_package_path(from_uuid, user, package_name);
649 auto to = create_data_user_ce_path(to_uuid, user);
650 argv[6] = (char*) from.c_str();
651 argv[7] = (char*) to.c_str();
652
653 LOG(DEBUG) << "Copying " << from << " to " << to;
654 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
655 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700656 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkey51c94492016-05-10 17:46:39 -0600657 goto fail;
658 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700659 }
660
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700661 if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE,
662 appId, seInfo).isOk()) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700663 res = error("Failed to restorecon");
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700664 goto fail;
665 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700666 }
667
Jeff Sharkey31f08982015-07-07 13:31:37 -0700668 // We let the framework scan the new location and persist that before
669 // deleting the data in the old location; this ordering ensures that
670 // we can recover from things like battery pulls.
Jeff Sharkey423e7462016-12-09 18:18:43 -0700671 return ok();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700672
673fail:
674 // Nuke everything we might have already copied
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700675 {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600676 auto to = create_data_app_package_path(to_uuid, data_app_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700677 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
678 LOG(WARNING) << "Failed to rollback " << to;
679 }
680 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700681 for (auto user : users) {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600682 {
683 auto to = create_data_user_de_package_path(to_uuid, user, package_name);
684 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
685 LOG(WARNING) << "Failed to rollback " << to;
686 }
687 }
688 {
689 auto to = create_data_user_ce_package_path(to_uuid, user, package_name);
690 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
691 LOG(WARNING) << "Failed to rollback " << to;
692 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700693 }
694 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700695 return res;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700696}
697
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700698binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid,
699 int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) {
700 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700701 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700702 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700703
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700704 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600705 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700706 if (uuid_ == nullptr) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700707 if (ensure_config_user_dirs(userId) != 0) {
Jeff Sharkey7be5ead2016-12-12 13:18:46 -0700708 return error(StringPrintf("Failed to ensure dirs for %d", userId));
Jeff Sharkey423e7462016-12-09 18:18:43 -0700709 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700710 }
711 }
Jeff Sharkey7be5ead2016-12-12 13:18:46 -0700712 return ok();
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700713}
714
715binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid,
716 int32_t userId, int32_t flags) {
717 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700718 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700719 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700720
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700721 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey423e7462016-12-09 18:18:43 -0700722 binder::Status res = ok();
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700723 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700724 auto path = create_data_user_de_path(uuid_, userId);
725 if (delete_dir_contents_and_dir(path, true) != 0) {
726 res = error("Failed to delete " + path);
727 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700728 if (uuid_ == nullptr) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700729 path = create_data_misc_legacy_path(userId);
730 if (delete_dir_contents_and_dir(path, true) != 0) {
731 res = error("Failed to delete " + path);
732 }
Calin Juravle114f0812017-03-08 19:05:07 -0800733 path = create_primary_cur_profile_dir_path(userId);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700734 if (delete_dir_contents_and_dir(path, true) != 0) {
735 res = error("Failed to delete " + path);
736 }
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700737 }
Robin Lee095c7632014-04-25 15:05:19 +0100738 }
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600739 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700740 auto path = create_data_user_ce_path(uuid_, userId);
741 if (delete_dir_contents_and_dir(path, true) != 0) {
742 res = error("Failed to delete " + path);
743 }
744 path = create_data_media_path(uuid_, userId);
745 if (delete_dir_contents_and_dir(path, true) != 0) {
746 res = error("Failed to delete " + path);
747 }
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600748 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700749 return res;
Robin Lee095c7632014-04-25 15:05:19 +0100750}
751
Mike Lockwood94afecf2012-10-24 10:45:23 -0700752/* Try to ensure free_size bytes of storage are available.
753 * Returns 0 on success.
754 * This is rather simple-minded because doing a full LRU would
755 * be potentially memory-intensive, and without atime it would
756 * also require that apps constantly modify file metadata even
757 * when just reading from the cache, which is pretty awful.
758 */
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700759binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid,
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700760 int64_t freeStorageSize, int32_t flags) {
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700761 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700762 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700763 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700764
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700765 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700766 auto data_path = create_data_path(uuid_);
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700767 auto device = findQuotaDeviceForUuid(uuid);
768 auto noop = (flags & FLAG_FREE_CACHE_NOOP);
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700769
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700770 int64_t free = data_disk_free(data_path);
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700771 if (free < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700772 return error("Failed to determine free space for " + data_path);
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700773 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700774
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600775 int64_t needed = freeStorageSize - free;
776 LOG(DEBUG) << "Device " << data_path << " has " << free << " free; requested "
777 << freeStorageSize << "; needed " << needed;
778
779 if (free >= freeStorageSize) {
780 return ok();
781 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700782
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700783 if (flags & FLAG_FREE_CACHE_V2) {
784 // This new cache strategy fairly removes files from UIDs by deleting
785 // files from the UIDs which are most over their allocated quota
786
787 // 1. Create trackers for every known UID
788 ATRACE_BEGIN("create");
789 std::unordered_map<uid_t, std::shared_ptr<CacheTracker>> trackers;
790 for (auto user : get_known_users(uuid_)) {
791 FTS *fts;
792 FTSENT *p;
793 char *argv[] = {
794 (char*) create_data_user_ce_path(uuid_, user).c_str(),
795 (char*) create_data_user_de_path(uuid_, user).c_str(),
796 nullptr
797 };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700798 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700799 return error("Failed to fts_open");
800 }
801 while ((p = fts_read(fts)) != NULL) {
802 if (p->fts_info == FTS_D && p->fts_level == 1) {
803 uid_t uid = p->fts_statp->st_uid;
804 auto search = trackers.find(uid);
805 if (search != trackers.end()) {
806 search->second->addDataPath(p->fts_path);
807 } else {
808 auto tracker = std::shared_ptr<CacheTracker>(new CacheTracker(
809 multiuser_get_user_id(uid), multiuser_get_app_id(uid), device));
810 tracker->addDataPath(p->fts_path);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700811 {
812 std::lock_guard<std::recursive_mutex> lock(mCacheQuotasLock);
813 tracker->cacheQuota = mCacheQuotas[uid];
814 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700815 if (tracker->cacheQuota == 0) {
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600816#if MEASURE_DEBUG
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700817 LOG(WARNING) << "UID " << uid << " has no cache quota; assuming 64MB";
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600818#endif
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700819 tracker->cacheQuota = 67108864;
820 }
821 trackers[uid] = tracker;
822 }
823 fts_set(fts, p, FTS_SKIP);
824 }
825 }
826 fts_close(fts);
827 }
828 ATRACE_END();
829
830 // 2. Populate tracker stats and insert into priority queue
831 ATRACE_BEGIN("populate");
832 auto cmp = [](std::shared_ptr<CacheTracker> left, std::shared_ptr<CacheTracker> right) {
833 return (left->getCacheRatio() < right->getCacheRatio());
834 };
835 std::priority_queue<std::shared_ptr<CacheTracker>,
836 std::vector<std::shared_ptr<CacheTracker>>, decltype(cmp)> queue(cmp);
837 for (const auto& it : trackers) {
838 it.second->loadStats();
839 queue.push(it.second);
840 }
841 ATRACE_END();
842
843 // 3. Bounce across the queue, freeing items from whichever tracker is
844 // the most over their assigned quota
845 ATRACE_BEGIN("bounce");
846 std::shared_ptr<CacheTracker> active;
847 while (active || !queue.empty()) {
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700848 // Only look at apps under quota when explicitly requested
849 if (active && (active->getCacheRatio() < 10000)
850 && !(flags & FLAG_FREE_CACHE_V2_DEFY_QUOTA)) {
851 LOG(DEBUG) << "Active ratio " << active->getCacheRatio()
852 << " isn't over quota, and defy not requested";
853 break;
854 }
855
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700856 // Find the best tracker to work with; this might involve swapping
857 // if the active tracker is no longer the most over quota
858 bool nextBetter = active && !queue.empty()
859 && active->getCacheRatio() < queue.top()->getCacheRatio();
860 if (!active || nextBetter) {
861 if (active) {
862 // Current tracker still has items, so we'll consider it
863 // again later once it bubbles up to surface
864 queue.push(active);
865 }
866 active = queue.top(); queue.pop();
867 active->ensureItems();
868 continue;
869 }
870
871 // If no items remain, go find another tracker
872 if (active->items.empty()) {
873 active = nullptr;
874 continue;
875 } else {
876 auto item = active->items.back();
877 active->items.pop_back();
878
879 LOG(DEBUG) << "Purging " << item->toString() << " from " << active->toString();
880 if (!noop) {
881 item->purge();
882 }
883 active->cacheUsed -= item->size;
884 needed -= item->size;
885 }
886
887 // Verify that we're actually done before bailing, since sneaky
888 // apps might be using hardlinks
889 if (needed <= 0) {
890 free = data_disk_free(data_path);
891 needed = freeStorageSize - free;
892 if (needed <= 0) {
893 break;
894 } else {
895 LOG(WARNING) << "Expected to be done but still need " << needed;
896 }
897 }
898 }
899 ATRACE_END();
900
901 } else {
Jeff Sharkey1cb4aaf2017-03-27 16:41:06 -0600902 return error("Legacy cache logic no longer supported");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700903 }
904
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700905 free = data_disk_free(data_path);
906 if (free >= freeStorageSize) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700907 return ok();
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700908 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700909 return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64,
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700910 freeStorageSize, data_path.c_str(), free));
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700911 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700912}
913
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700914binder::Status InstalldNativeService::rmdex(const std::string& codePath,
915 const std::string& instructionSet) {
916 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700917 std::lock_guard<std::recursive_mutex> lock(mLock);
918
Mike Lockwood94afecf2012-10-24 10:45:23 -0700919 char dex_path[PKG_PATH_MAX];
920
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700921 const char* path = codePath.c_str();
922 const char* instruction_set = instructionSet.c_str();
923
Jeff Sharkey770180a2014-09-08 17:14:26 -0700924 if (validate_apk_path(path) && validate_system_app_path(path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700925 return error("Invalid path " + codePath);
Jeff Sharkey770180a2014-09-08 17:14:26 -0700926 }
927
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700928 if (!create_cache_path(dex_path, path, instruction_set)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700929 return error("Failed to create cache path for " + codePath);
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700930 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700931
932 ALOGV("unlink %s\n", dex_path);
933 if (unlink(dex_path) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700934 return error(StringPrintf("Failed to unlink %s", dex_path));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700935 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700936 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700937 }
938}
939
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700940struct stats {
941 int64_t codeSize;
942 int64_t dataSize;
943 int64_t cacheSize;
944};
945
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700946#if MEASURE_DEBUG
947static std::string toString(std::vector<int64_t> values) {
948 std::stringstream res;
949 res << "[";
950 for (size_t i = 0; i < values.size(); i++) {
951 res << values[i];
952 if (i < values.size() - 1) {
953 res << ",";
954 }
955 }
956 res << "]";
957 return res.str();
958}
959#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700960
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700961static void collectQuotaStats(const std::string& device, int32_t userId,
Jeff Sharkey7bf5b952017-01-19 09:21:36 -0700962 int32_t appId, struct stats* stats, struct stats* extStats) {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700963 if (device.empty()) return;
964
965 struct dqblk dq;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700966
967 uid_t uid = multiuser_get_uid(userId, appId);
968 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
969 reinterpret_cast<char*>(&dq)) != 0) {
970 if (errno != ESRCH) {
971 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
972 }
973 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700974#if MEASURE_DEBUG
975 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
976#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700977 stats->dataSize += dq.dqb_curspace;
978 }
979
980 int cacheGid = multiuser_get_cache_gid(userId, appId);
981 if (cacheGid != -1) {
982 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid,
983 reinterpret_cast<char*>(&dq)) != 0) {
984 if (errno != ESRCH) {
985 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid;
986 }
987 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700988#if MEASURE_DEBUG
Jeff Sharkey7bf5b952017-01-19 09:21:36 -0700989 LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700990#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700991 stats->cacheSize += dq.dqb_curspace;
992 }
993 }
994
Jeff Sharkey7bf5b952017-01-19 09:21:36 -0700995 int extGid = multiuser_get_ext_gid(userId, appId);
996 if (extGid != -1) {
997 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), extGid,
998 reinterpret_cast<char*>(&dq)) != 0) {
999 if (errno != ESRCH) {
1000 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << extGid;
1001 }
1002 } else {
1003#if MEASURE_DEBUG
1004 LOG(DEBUG) << "quotactl() for GID " << extGid << " " << dq.dqb_curspace;
1005#endif
1006 extStats->dataSize += dq.dqb_curspace;
1007 }
1008 }
1009
1010 int sharedGid = multiuser_get_shared_gid(userId, appId);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001011 if (sharedGid != -1) {
1012 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid,
1013 reinterpret_cast<char*>(&dq)) != 0) {
1014 if (errno != ESRCH) {
1015 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid;
1016 }
1017 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001018#if MEASURE_DEBUG
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001019 LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001020#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001021 stats->codeSize += dq.dqb_curspace;
1022 }
1023 }
1024}
1025
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001026static void collectManualStats(const std::string& path, struct stats* stats) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001027 DIR *d;
1028 int dfd;
1029 struct dirent *de;
1030 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001031
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001032 d = opendir(path.c_str());
1033 if (d == nullptr) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001034 if (errno != ENOENT) {
1035 PLOG(WARNING) << "Failed to open " << path;
1036 }
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001037 return;
1038 }
1039 dfd = dirfd(d);
1040 while ((de = readdir(d))) {
1041 const char *name = de->d_name;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001042
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001043 int64_t size = 0;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001044 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001045 size = s.st_blocks * 512;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001046 }
1047
1048 if (de->d_type == DT_DIR) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001049 if (!strcmp(name, ".")) {
1050 // Don't recurse, but still count node size
1051 } else if (!strcmp(name, "..")) {
1052 // Don't recurse or count node size
1053 continue;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001054 } else {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001055 // Measure all children nodes
1056 size = 0;
1057 calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001058 }
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001059
1060 if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) {
1061 stats->cacheSize += size;
1062 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001063 }
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001064
1065 // Legacy symlink isn't owned by app
1066 if (de->d_type == DT_LNK && !strcmp(name, "lib")) {
1067 continue;
1068 }
1069
1070 // Everything found inside is considered data
1071 stats->dataSize += size;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001072 }
1073 closedir(d);
1074}
1075
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001076static void collectManualStatsForUser(const std::string& path, struct stats* stats,
1077 bool exclude_apps = false) {
1078 DIR *d;
1079 int dfd;
1080 struct dirent *de;
1081 struct stat s;
1082
1083 d = opendir(path.c_str());
1084 if (d == nullptr) {
1085 if (errno != ENOENT) {
1086 PLOG(WARNING) << "Failed to open " << path;
1087 }
1088 return;
1089 }
1090 dfd = dirfd(d);
1091 while ((de = readdir(d))) {
1092 if (de->d_type == DT_DIR) {
1093 const char *name = de->d_name;
1094 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) {
1095 continue;
1096 }
1097 if (!strcmp(name, ".") || !strcmp(name, "..")) {
1098 continue;
1099 } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) {
1100 continue;
1101 } else {
1102 collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats);
1103 }
1104 }
1105 }
1106 closedir(d);
1107}
1108
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001109static void collectManualExternalStatsForUser(const std::string& path, struct stats* stats) {
1110 FTS *fts;
1111 FTSENT *p;
1112 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001113 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001114 PLOG(ERROR) << "Failed to fts_open " << path;
1115 return;
1116 }
1117 while ((p = fts_read(fts)) != NULL) {
Jeff Sharkey76ddaeb2017-01-25 22:15:42 -07001118 p->fts_number = p->fts_parent->fts_number;
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001119 switch (p->fts_info) {
1120 case FTS_D:
1121 if (p->fts_level == 4
1122 && !strcmp(p->fts_name, "cache")
1123 && !strcmp(p->fts_parent->fts_parent->fts_name, "data")
1124 && !strcmp(p->fts_parent->fts_parent->fts_parent->fts_name, "Android")) {
1125 p->fts_number = 1;
1126 }
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001127 // Fall through to count the directory
1128 case FTS_DEFAULT:
1129 case FTS_F:
1130 case FTS_SL:
1131 case FTS_SLNONE:
1132 int64_t size = (p->fts_statp->st_blocks * 512);
1133 if (p->fts_number == 1) {
1134 stats->cacheSize += size;
1135 }
1136 stats->dataSize += size;
1137 break;
1138 }
1139 }
1140 fts_close(fts);
1141}
1142
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001143binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001144 const std::vector<std::string>& packageNames, int32_t userId, int32_t flags,
1145 int32_t appId, const std::vector<int64_t>& ceDataInodes,
1146 const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) {
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001147 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001148 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001149 for (auto packageName : packageNames) {
1150 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
1151 }
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001152 // NOTE: Locking is relaxed on this method, since it's limited to
1153 // read-only measurements without mutation.
Jeff Sharkey423e7462016-12-09 18:18:43 -07001154
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001155 // When modifying this logic, always verify using tests:
1156 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize
1157
1158#if MEASURE_DEBUG
1159 LOG(INFO) << "Measuring user " << userId << " app " << appId;
1160#endif
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001161
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001162 // Here's a summary of the common storage locations across the platform,
1163 // and how they're each tagged:
1164 //
1165 // /data/app/com.example UID system
1166 // /data/app/com.example/oat UID system
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001167 // /data/user/0/com.example UID u0_a10 GID u0_a10
1168 // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache
1169 // /data/media/0/foo.txt UID u0_media_rw
1170 // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image
1171 // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext
1172 // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache
1173 // /data/media/obb/com.example UID system
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001174
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001175 struct stats stats;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001176 struct stats extStats;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001177 memset(&stats, 0, sizeof(stats));
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001178 memset(&extStats, 0, sizeof(extStats));
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001179
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001180 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001181
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001182 auto device = findQuotaDeviceForUuid(uuid);
1183 if (device.empty()) {
1184 flags &= ~FLAG_USE_QUOTA;
1185 }
1186
Jeff Sharkey466459b2017-01-17 15:25:01 -07001187 ATRACE_BEGIN("obb");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001188 for (auto packageName : packageNames) {
1189 auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str());
1190 calculate_tree_size(obbCodePath, &extStats.codeSize);
1191 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001192 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001193
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001194 if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001195 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001196 for (auto codePath : codePaths) {
1197 calculate_tree_size(codePath, &stats.codeSize, -1,
1198 multiuser_get_shared_gid(userId, appId));
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001199 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001200 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001201
Jeff Sharkey466459b2017-01-17 15:25:01 -07001202 ATRACE_BEGIN("quota");
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001203 collectQuotaStats(device, userId, appId, &stats, &extStats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001204 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001205
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001206 } else {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001207 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001208 for (auto codePath : codePaths) {
1209 calculate_tree_size(codePath, &stats.codeSize);
1210 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001211 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001212
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001213 for (size_t i = 0; i < packageNames.size(); i++) {
1214 const char* pkgname = packageNames[i].c_str();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001215
Jeff Sharkey466459b2017-01-17 15:25:01 -07001216 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001217 auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]);
1218 collectManualStats(cePath, &stats);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001219 auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname);
1220 collectManualStats(dePath, &stats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001221 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001222
Jeff Sharkey466459b2017-01-17 15:25:01 -07001223 ATRACE_BEGIN("profiles");
Calin Juravle114f0812017-03-08 19:05:07 -08001224 auto userProfilePath = create_primary_current_profile_package_dir_path(userId, pkgname);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001225 calculate_tree_size(userProfilePath, &stats.dataSize);
Calin Juravle114f0812017-03-08 19:05:07 -08001226 auto refProfilePath = create_primary_reference_profile_package_dir_path(pkgname);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001227 calculate_tree_size(refProfilePath, &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001228 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001229
Jeff Sharkey466459b2017-01-17 15:25:01 -07001230 ATRACE_BEGIN("external");
Jeff Sharkey76ddaeb2017-01-25 22:15:42 -07001231 auto extPath = create_data_media_package_path(uuid_, userId, "data", pkgname);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001232 collectManualStats(extPath, &extStats);
Jeff Sharkey76ddaeb2017-01-25 22:15:42 -07001233 auto mediaPath = create_data_media_package_path(uuid_, userId, "media", pkgname);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001234 calculate_tree_size(mediaPath, &extStats.dataSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001235 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001236 }
1237
Jeff Sharkey466459b2017-01-17 15:25:01 -07001238 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001239 int32_t sharedGid = multiuser_get_shared_gid(userId, appId);
1240 if (sharedGid != -1) {
1241 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize,
1242 sharedGid, -1);
1243 }
Calin Juravle114f0812017-03-08 19:05:07 -08001244 calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001245 multiuser_get_uid(userId, appId), -1);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001246 ATRACE_END();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001247 }
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001248
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001249 std::vector<int64_t> ret;
1250 ret.push_back(stats.codeSize);
1251 ret.push_back(stats.dataSize);
1252 ret.push_back(stats.cacheSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001253 ret.push_back(extStats.codeSize);
1254 ret.push_back(extStats.dataSize);
1255 ret.push_back(extStats.cacheSize);
1256#if MEASURE_DEBUG
1257 LOG(DEBUG) << "Final result " << toString(ret);
1258#endif
1259 *_aidl_return = ret;
1260 return ok();
1261}
1262
1263binder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid,
1264 int32_t userId, int32_t flags, const std::vector<int32_t>& appIds,
1265 std::vector<int64_t>* _aidl_return) {
1266 ENFORCE_UID(AID_SYSTEM);
1267 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001268 // NOTE: Locking is relaxed on this method, since it's limited to
1269 // read-only measurements without mutation.
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001270
1271 // When modifying this logic, always verify using tests:
1272 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize
1273
1274#if MEASURE_DEBUG
1275 LOG(INFO) << "Measuring user " << userId;
1276#endif
1277
1278 struct stats stats;
1279 struct stats extStats;
1280 memset(&stats, 0, sizeof(stats));
1281 memset(&extStats, 0, sizeof(extStats));
1282
1283 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1284
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001285 auto device = findQuotaDeviceForUuid(uuid);
1286 if (device.empty()) {
1287 flags &= ~FLAG_USE_QUOTA;
1288 }
1289
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001290 if (flags & FLAG_USE_QUOTA) {
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001291 struct dqblk dq;
1292
1293 ATRACE_BEGIN("obb");
1294 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), AID_MEDIA_OBB,
1295 reinterpret_cast<char*>(&dq)) != 0) {
1296 if (errno != ESRCH) {
1297 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << AID_MEDIA_OBB;
1298 }
1299 } else {
1300#if MEASURE_DEBUG
1301 LOG(DEBUG) << "quotactl() for GID " << AID_MEDIA_OBB << " " << dq.dqb_curspace;
1302#endif
1303 extStats.codeSize += dq.dqb_curspace;
1304 }
1305 ATRACE_END();
1306
Jeff Sharkey466459b2017-01-17 15:25:01 -07001307 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001308 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001309 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001310
Jeff Sharkey466459b2017-01-17 15:25:01 -07001311 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001312 auto cePath = create_data_user_ce_path(uuid_, userId);
1313 collectManualStatsForUser(cePath, &stats, true);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001314 auto dePath = create_data_user_de_path(uuid_, userId);
1315 collectManualStatsForUser(dePath, &stats, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001316 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001317
Jeff Sharkey466459b2017-01-17 15:25:01 -07001318 ATRACE_BEGIN("profile");
Calin Juravle114f0812017-03-08 19:05:07 -08001319 auto userProfilePath = create_primary_cur_profile_dir_path(userId);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001320 calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true);
Calin Juravle114f0812017-03-08 19:05:07 -08001321 auto refProfilePath = create_primary_ref_profile_dir_path();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001322 calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001323 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001324
Jeff Sharkey466459b2017-01-17 15:25:01 -07001325 ATRACE_BEGIN("external");
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001326 uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW);
1327 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
1328 reinterpret_cast<char*>(&dq)) != 0) {
1329 if (errno != ESRCH) {
1330 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
1331 }
1332 } else {
1333#if MEASURE_DEBUG
1334 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001335#endif
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001336 extStats.dataSize += dq.dqb_curspace;
1337 }
1338 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001339
Jeff Sharkey466459b2017-01-17 15:25:01 -07001340 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001341 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize,
1342 -1, -1, true);
Calin Juravle114f0812017-03-08 19:05:07 -08001343 calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001344 -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001345 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001346
Jeff Sharkey466459b2017-01-17 15:25:01 -07001347 ATRACE_BEGIN("quota");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001348 for (auto appId : appIds) {
1349 if (appId >= AID_APP_START) {
1350 collectQuotaStats(device, userId, appId, &stats, &extStats);
1351#if MEASURE_DEBUG
1352 // Sleep to make sure we don't lose logs
1353 usleep(1);
1354#endif
1355 }
1356 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001357 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001358 } else {
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001359 ATRACE_BEGIN("obb");
1360 auto obbPath = create_data_path(uuid_) + "/media/obb";
1361 calculate_tree_size(obbPath, &extStats.codeSize);
1362 ATRACE_END();
1363
Jeff Sharkey466459b2017-01-17 15:25:01 -07001364 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001365 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001366 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001367
Jeff Sharkey466459b2017-01-17 15:25:01 -07001368 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001369 auto cePath = create_data_user_ce_path(uuid_, userId);
1370 collectManualStatsForUser(cePath, &stats);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001371 auto dePath = create_data_user_de_path(uuid_, userId);
1372 collectManualStatsForUser(dePath, &stats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001373 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001374
Jeff Sharkey466459b2017-01-17 15:25:01 -07001375 ATRACE_BEGIN("profile");
Calin Juravle114f0812017-03-08 19:05:07 -08001376 auto userProfilePath = create_primary_cur_profile_dir_path(userId);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001377 calculate_tree_size(userProfilePath, &stats.dataSize);
Calin Juravle114f0812017-03-08 19:05:07 -08001378 auto refProfilePath = create_primary_ref_profile_dir_path();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001379 calculate_tree_size(refProfilePath, &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001380 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001381
Jeff Sharkey466459b2017-01-17 15:25:01 -07001382 ATRACE_BEGIN("external");
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001383 auto dataMediaPath = create_data_media_path(uuid_, userId);
1384 collectManualExternalStatsForUser(dataMediaPath, &extStats);
1385#if MEASURE_DEBUG
1386 LOG(DEBUG) << "Measured external data " << extStats.dataSize << " cache "
1387 << extStats.cacheSize;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001388#endif
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001389 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001390
Jeff Sharkey466459b2017-01-17 15:25:01 -07001391 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001392 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize);
Calin Juravle114f0812017-03-08 19:05:07 -08001393 calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001394 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001395 }
1396
1397 std::vector<int64_t> ret;
1398 ret.push_back(stats.codeSize);
1399 ret.push_back(stats.dataSize);
1400 ret.push_back(stats.cacheSize);
1401 ret.push_back(extStats.codeSize);
1402 ret.push_back(extStats.dataSize);
1403 ret.push_back(extStats.cacheSize);
1404#if MEASURE_DEBUG
1405 LOG(DEBUG) << "Final result " << toString(ret);
1406#endif
1407 *_aidl_return = ret;
1408 return ok();
1409}
1410
1411binder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid,
1412 int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) {
1413 ENFORCE_UID(AID_SYSTEM);
1414 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001415 // NOTE: Locking is relaxed on this method, since it's limited to
1416 // read-only measurements without mutation.
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001417
1418 // When modifying this logic, always verify using tests:
1419 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize
1420
1421#if MEASURE_DEBUG
1422 LOG(INFO) << "Measuring external " << userId;
1423#endif
1424
1425 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1426
1427 int64_t totalSize = 0;
1428 int64_t audioSize = 0;
1429 int64_t videoSize = 0;
1430 int64_t imageSize = 0;
1431
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001432 auto device = findQuotaDeviceForUuid(uuid);
1433 if (device.empty()) {
1434 flags &= ~FLAG_USE_QUOTA;
1435 }
1436
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001437 if (flags & FLAG_USE_QUOTA) {
1438 struct dqblk dq;
1439
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001440 uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW);
1441 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
1442 reinterpret_cast<char*>(&dq)) != 0) {
1443 if (errno != ESRCH) {
1444 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
1445 }
1446 } else {
1447#if MEASURE_DEBUG
1448 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
1449#endif
1450 totalSize = dq.dqb_curspace;
1451 }
1452
1453 gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO);
1454 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid,
1455 reinterpret_cast<char*>(&dq)) == 0) {
1456#if MEASURE_DEBUG
1457 LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace;
1458#endif
1459 audioSize = dq.dqb_curspace;
1460 }
1461 gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO);
1462 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid,
1463 reinterpret_cast<char*>(&dq)) == 0) {
1464#if MEASURE_DEBUG
1465 LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace;
1466#endif
1467 videoSize = dq.dqb_curspace;
1468 }
1469 gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE);
1470 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid,
1471 reinterpret_cast<char*>(&dq)) == 0) {
1472#if MEASURE_DEBUG
1473 LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace;
1474#endif
1475 imageSize = dq.dqb_curspace;
1476 }
1477 } else {
1478 FTS *fts;
1479 FTSENT *p;
1480 auto path = create_data_media_path(uuid_, userId);
1481 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001482 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001483 return error("Failed to fts_open " + path);
1484 }
1485 while ((p = fts_read(fts)) != NULL) {
1486 char* ext;
1487 int64_t size = (p->fts_statp->st_blocks * 512);
1488 switch (p->fts_info) {
1489 case FTS_F:
1490 // Only categorize files not belonging to apps
Jeff Sharkey444ad1e2017-03-12 16:25:36 -06001491 if (p->fts_parent->fts_number == 0) {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001492 ext = strrchr(p->fts_name, '.');
1493 if (ext != nullptr) {
1494 switch (MatchExtension(++ext)) {
1495 case AID_MEDIA_AUDIO: audioSize += size; break;
1496 case AID_MEDIA_VIDEO: videoSize += size; break;
1497 case AID_MEDIA_IMAGE: imageSize += size; break;
1498 }
1499 }
1500 }
1501 // Fall through to always count against total
1502 case FTS_D:
Jeff Sharkey444ad1e2017-03-12 16:25:36 -06001503 // Ignore data belonging to specific apps
1504 p->fts_number = p->fts_parent->fts_number;
1505 if (p->fts_level == 1 && !strcmp(p->fts_name, "Android")) {
1506 p->fts_number = 1;
1507 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001508 case FTS_DEFAULT:
1509 case FTS_SL:
1510 case FTS_SLNONE:
1511 totalSize += size;
1512 break;
1513 }
1514 }
1515 fts_close(fts);
1516 }
1517
1518 std::vector<int64_t> ret;
1519 ret.push_back(totalSize);
1520 ret.push_back(audioSize);
1521 ret.push_back(videoSize);
1522 ret.push_back(imageSize);
1523#if MEASURE_DEBUG
1524 LOG(DEBUG) << "Final result " << toString(ret);
1525#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001526 *_aidl_return = ret;
Jeff Sharkey423e7462016-12-09 18:18:43 -07001527 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001528}
1529
Jeff Sharkey88ddd942017-01-17 18:05:54 -07001530binder::Status InstalldNativeService::setAppQuota(const std::unique_ptr<std::string>& uuid,
1531 int32_t userId, int32_t appId, int64_t cacheQuota) {
1532 ENFORCE_UID(AID_SYSTEM);
1533 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001534 std::lock_guard<std::recursive_mutex> lock(mCacheQuotasLock);
Jeff Sharkey88ddd942017-01-17 18:05:54 -07001535
1536 int32_t uid = multiuser_get_uid(userId, appId);
1537 mCacheQuotas[uid] = cacheQuota;
1538
1539 return ok();
1540}
1541
David Sehr6727c2d2016-05-17 16:06:22 -07001542// Dumps the contents of a profile file, using pkgname's dex files for pretty
1543// printing the result.
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001544binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName,
1545 const std::string& codePaths, bool* _aidl_return) {
1546 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001547 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001548 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001549
1550 const char* pkgname = packageName.c_str();
Jeff Sharkey90aff262016-12-12 14:28:24 -07001551 const char* code_paths = codePaths.c_str();
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001552
Jeff Sharkey90aff262016-12-12 14:28:24 -07001553 *_aidl_return = dump_profiles(uid, pkgname, code_paths);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001554 return ok();
David Sehr6727c2d2016-05-17 16:06:22 -07001555}
1556
Andreas Gampe4d0f8252016-03-20 11:30:28 -07001557// TODO: Consider returning error codes.
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001558binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName,
Jeff Sharkey423e7462016-12-09 18:18:43 -07001559 bool* _aidl_return) {
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001560 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001561 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001562 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001563
Calin Juravle114f0812017-03-08 19:05:07 -08001564 *_aidl_return = analyze_primary_profiles(uid, packageName);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001565 return ok();
Andreas Gampe4d0f8252016-03-20 11:30:28 -07001566}
1567
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001568binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid,
1569 const std::unique_ptr<std::string>& packageName, const std::string& instructionSet,
1570 int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags,
1571 const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid,
1572 const std::unique_ptr<std::string>& sharedLibraries) {
1573 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001574 CHECK_ARGUMENT_UUID(uuid);
1575 if (packageName && *packageName != "*") {
1576 CHECK_ARGUMENT_PACKAGE_NAME(*packageName);
1577 }
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001578 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001579
1580 const char* apk_path = apkPath.c_str();
1581 const char* pkgname = packageName ? packageName->c_str() : "*";
1582 const char* instruction_set = instructionSet.c_str();
1583 const char* oat_dir = outputPath ? outputPath->c_str() : nullptr;
1584 const char* compiler_filter = compilerFilter.c_str();
1585 const char* volume_uuid = uuid ? uuid->c_str() : nullptr;
1586 const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr;
1587
1588 int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded,
1589 oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001590 return res ? error(res, "Failed to dexopt") : ok();
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001591}
1592
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001593binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) {
1594 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001595 std::lock_guard<std::recursive_mutex> lock(mLock);
1596
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001597 const char* instruction_set = instructionSet.c_str();
1598
1599 char boot_marker_path[PKG_PATH_MAX];
1600 sprintf(boot_marker_path,
Andreas Gampe02d0de52015-11-11 20:43:16 -08001601 "%s/%s/%s/.booting",
1602 android_data_dir.path,
1603 DALVIK_CACHE,
1604 instruction_set);
Narayan Kamath091ea772014-11-10 15:03:46 +00001605
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001606 ALOGV("mark_boot_complete : %s", boot_marker_path);
1607 if (unlink(boot_marker_path) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001608 return error(StringPrintf("Failed to unlink %s", boot_marker_path));
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001609 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001610 return ok();
Narayan Kamath091ea772014-11-10 15:03:46 +00001611}
1612
Mike Lockwood94afecf2012-10-24 10:45:23 -07001613void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1614 struct stat* statbuf)
1615{
1616 while (path[basepos] != 0) {
1617 if (path[basepos] == '/') {
1618 path[basepos] = 0;
1619 if (lstat(path, statbuf) < 0) {
1620 ALOGV("Making directory: %s\n", path);
1621 if (mkdir(path, mode) == 0) {
1622 chown(path, uid, gid);
1623 } else {
1624 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1625 }
1626 }
1627 path[basepos] = '/';
1628 basepos++;
1629 }
1630 basepos++;
1631 }
1632}
1633
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001634binder::Status InstalldNativeService::linkNativeLibraryDirectory(
1635 const std::unique_ptr<std::string>& uuid, const std::string& packageName,
1636 const std::string& nativeLibPath32, int32_t userId) {
1637 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001638 CHECK_ARGUMENT_UUID(uuid);
1639 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001640 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001641
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001642 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1643 const char* pkgname = packageName.c_str();
1644 const char* asecLibDir = nativeLibPath32.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001645 struct stat s, libStat;
Jeff Sharkey423e7462016-12-09 18:18:43 -07001646 binder::Status res = ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001647
Jeff Sharkey423e7462016-12-09 18:18:43 -07001648 auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname);
1649 auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX;
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001650
1651 const char* pkgdir = _pkgdir.c_str();
1652 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001653
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001654 if (stat(pkgdir, &s) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001655 return error("Failed to stat " + _pkgdir);
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001656 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001657
1658 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001659 return error("Failed to chown " + _pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001660 }
1661
1662 if (chmod(pkgdir, 0700) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001663 res = error("Failed to chmod " + _pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001664 goto out;
1665 }
1666
1667 if (lstat(libsymlink, &libStat) < 0) {
1668 if (errno != ENOENT) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001669 res = error("Failed to stat " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001670 goto out;
1671 }
1672 } else {
1673 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001674 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001675 res = error("Failed to delete " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001676 goto out;
1677 }
1678 } else if (S_ISLNK(libStat.st_mode)) {
1679 if (unlink(libsymlink) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001680 res = error("Failed to unlink " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001681 goto out;
1682 }
1683 }
1684 }
1685
1686 if (symlink(asecLibDir, libsymlink) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001687 res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001688 goto out;
1689 }
1690
1691out:
1692 if (chmod(pkgdir, s.st_mode) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001693 auto msg = "Failed to cleanup chmod " + _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
1701 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001702 auto msg = "Failed to cleanup chown " + _pkgdir;
1703 if (res.isOk()) {
1704 res = error(msg);
1705 } else {
1706 PLOG(ERROR) << msg;
1707 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001708 }
1709
Jeff Sharkey423e7462016-12-09 18:18:43 -07001710 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001711}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001712
1713static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1714{
1715 static const char *IDMAP_BIN = "/system/bin/idmap";
1716 static const size_t MAX_INT_LEN = 32;
1717 char idmap_str[MAX_INT_LEN];
1718
1719 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1720
1721 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1722 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1723}
1724
1725// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1726// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1727static int flatten_path(const char *prefix, const char *suffix,
1728 const char *overlay_path, char *idmap_path, size_t N)
1729{
1730 if (overlay_path == NULL || idmap_path == NULL) {
1731 return -1;
1732 }
1733 const size_t len_overlay_path = strlen(overlay_path);
1734 // will access overlay_path + 1 further below; requires absolute path
1735 if (len_overlay_path < 2 || *overlay_path != '/') {
1736 return -1;
1737 }
1738 const size_t len_idmap_root = strlen(prefix);
1739 const size_t len_suffix = strlen(suffix);
1740 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1741 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1742 // additions below would cause overflow
1743 return -1;
1744 }
1745 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1746 return -1;
1747 }
1748 memset(idmap_path, 0, N);
1749 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1750 char *ch = idmap_path + len_idmap_root;
1751 while (*ch != '\0') {
1752 if (*ch == '/') {
1753 *ch = '@';
1754 }
1755 ++ch;
1756 }
1757 return 0;
1758}
1759
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001760binder::Status InstalldNativeService::idmap(const std::string& targetApkPath,
1761 const std::string& overlayApkPath, int32_t uid) {
1762 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001763 std::lock_guard<std::recursive_mutex> lock(mLock);
1764
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001765 const char* target_apk = targetApkPath.c_str();
1766 const char* overlay_apk = overlayApkPath.c_str();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001767 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1768
1769 int idmap_fd = -1;
1770 char idmap_path[PATH_MAX];
1771
1772 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1773 idmap_path, sizeof(idmap_path)) == -1) {
1774 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1775 goto fail;
1776 }
1777
1778 unlink(idmap_path);
1779 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1780 if (idmap_fd < 0) {
1781 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1782 goto fail;
1783 }
1784 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1785 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1786 goto fail;
1787 }
1788 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1789 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1790 goto fail;
1791 }
1792
1793 pid_t pid;
1794 pid = fork();
1795 if (pid == 0) {
1796 /* child -- drop privileges before continuing */
1797 if (setgid(uid) != 0) {
1798 ALOGE("setgid(%d) failed during idmap\n", uid);
1799 exit(1);
1800 }
1801 if (setuid(uid) != 0) {
1802 ALOGE("setuid(%d) failed during idmap\n", uid);
1803 exit(1);
1804 }
1805 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1806 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1807 exit(1);
1808 }
1809
1810 run_idmap(target_apk, overlay_apk, idmap_fd);
1811 exit(1); /* only if exec call to idmap failed */
1812 } else {
1813 int status = wait_child(pid);
1814 if (status != 0) {
1815 ALOGE("idmap failed, status=0x%04x\n", status);
1816 goto fail;
1817 }
1818 }
1819
1820 close(idmap_fd);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001821 return ok();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001822fail:
1823 if (idmap_fd >= 0) {
1824 close(idmap_fd);
1825 unlink(idmap_path);
1826 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001827 return error();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001828}
Robert Craige9887e42014-02-20 10:25:56 -05001829
MÃ¥rten Kongstad5c6944c2015-12-15 14:02:30 +01001830binder::Status InstalldNativeService::removeIdmap(const std::string& overlayApkPath) {
1831 const char* overlay_apk = overlayApkPath.c_str();
1832 char idmap_path[PATH_MAX];
1833
1834 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1835 idmap_path, sizeof(idmap_path)) == -1) {
1836 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1837 return error();
1838 }
1839 if (unlink(idmap_path) < 0) {
1840 ALOGE("couldn't unlink idmap file %s\n", idmap_path);
1841 return error();
1842 }
1843 return ok();
1844}
1845
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001846binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid,
1847 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
1848 const std::string& seInfo) {
1849 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001850 CHECK_ARGUMENT_UUID(uuid);
1851 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001852 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001853
1854 binder::Status res = ok();
Robert Craige9887e42014-02-20 10:25:56 -05001855
Robert Craigda30dc72014-03-27 10:21:12 -04001856 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001857 unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE;
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001858 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1859 const char* pkgName = packageName.c_str();
1860 const char* seinfo = seInfo.c_str();
Robert Craigda30dc72014-03-27 10:21:12 -04001861
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001862 uid_t uid = multiuser_get_uid(userId, appId);
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -07001863 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001864 auto path = create_data_user_ce_package_path(uuid_, userId, pkgName);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001865 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001866 res = error("restorecon failed for " + path);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001867 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001868 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -07001869 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001870 auto path = create_data_user_de_package_path(uuid_, userId, pkgName);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001871 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001872 res = error("restorecon failed for " + path);
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001873 }
Robert Craige9887e42014-02-20 10:25:56 -05001874 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001875 return res;
Robert Craige9887e42014-02-20 10:25:56 -05001876}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001877
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001878binder::Status InstalldNativeService::createOatDir(const std::string& oatDir,
1879 const std::string& instructionSet) {
1880 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001881 std::lock_guard<std::recursive_mutex> lock(mLock);
1882
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001883 const char* oat_dir = oatDir.c_str();
1884 const char* instruction_set = instructionSet.c_str();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001885 char oat_instr_dir[PKG_PATH_MAX];
1886
1887 if (validate_apk_path(oat_dir)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001888 return error("Invalid path " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001889 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001890 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001891 return error("Failed to prepare " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001892 }
1893 if (selinux_android_restorecon(oat_dir, 0)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001894 return error("Failed to restorecon " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001895 }
1896 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001897 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001898 return error(StringPrintf("Failed to prepare %s", oat_instr_dir));
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001899 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001900 return ok();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001901}
1902
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001903binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) {
1904 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001905 std::lock_guard<std::recursive_mutex> lock(mLock);
1906
Jeff Sharkey423e7462016-12-09 18:18:43 -07001907 if (validate_apk_path(packageDir.c_str())) {
1908 return error("Invalid path " + packageDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001909 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001910 if (delete_dir_contents_and_dir(packageDir) != 0) {
1911 return error("Failed to delete " + packageDir);
1912 }
1913 return ok();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001914}
1915
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001916binder::Status InstalldNativeService::linkFile(const std::string& relativePath,
1917 const std::string& fromBase, const std::string& toBase) {
1918 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001919 std::lock_guard<std::recursive_mutex> lock(mLock);
1920
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001921 const char* relative_path = relativePath.c_str();
1922 const char* from_base = fromBase.c_str();
1923 const char* to_base = toBase.c_str();
Narayan Kamathd845c962015-06-04 13:20:27 +01001924 char from_path[PKG_PATH_MAX];
1925 char to_path[PKG_PATH_MAX];
1926 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
1927 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
1928
1929 if (validate_apk_path_subdirs(from_path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001930 return error(StringPrintf("Invalid from path %s", from_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001931 }
1932
1933 if (validate_apk_path_subdirs(to_path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001934 return error(StringPrintf("Invalid to path %s", to_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001935 }
1936
Jeff Sharkey423e7462016-12-09 18:18:43 -07001937 if (link(from_path, to_path) < 0) {
1938 return error(StringPrintf("Failed to link from %s to %s", from_path, to_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001939 }
1940
Jeff Sharkey423e7462016-12-09 18:18:43 -07001941 return ok();
Narayan Kamathd845c962015-06-04 13:20:27 +01001942}
1943
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001944binder::Status InstalldNativeService::moveAb(const std::string& apkPath,
1945 const std::string& instructionSet, const std::string& outputPath) {
1946 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001947 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001948
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001949 const char* apk_path = apkPath.c_str();
1950 const char* instruction_set = instructionSet.c_str();
1951 const char* oat_dir = outputPath.c_str();
Andreas Gampe0354bd02016-06-27 14:25:30 -07001952
Jeff Sharkey90aff262016-12-12 14:28:24 -07001953 bool success = move_ab(apk_path, instruction_set, oat_dir);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001954 return success ? ok() : error();
Andreas Gampee65b4fa2016-03-01 11:46:45 -08001955}
1956
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001957binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath,
1958 const std::string& instructionSet, const std::string& outputPath) {
1959 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001960 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001961
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001962 const char* apk_path = apkPath.c_str();
1963 const char* instruction_set = instructionSet.c_str();
1964 const char* oat_dir = outputPath.c_str();
1965
Jeff Sharkey90aff262016-12-12 14:28:24 -07001966 bool res = delete_odex(apk_path, instruction_set, oat_dir);
1967 return res ? ok() : error();
Andreas Gampe3964da02016-09-09 17:07:04 -07001968}
1969
Calin Juravlebd968362017-01-25 01:17:17 -08001970binder::Status InstalldNativeService::reconcileSecondaryDexFile(
1971 const std::string& dexPath, const std::string& packageName, int32_t uid,
1972 const std::vector<std::string>& isas, const std::unique_ptr<std::string>& volumeUuid,
1973 int32_t storage_flag, bool* _aidl_return) {
1974 ENFORCE_UID(AID_SYSTEM);
1975 CHECK_ARGUMENT_UUID(volumeUuid);
1976 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
1977
1978 std::lock_guard<std::recursive_mutex> lock(mLock);
1979 bool result = android::installd::reconcile_secondary_dex_file(
1980 dexPath, packageName, uid, isas, volumeUuid, storage_flag, _aidl_return);
1981 return result ? ok() : error();
1982}
1983
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001984binder::Status InstalldNativeService::invalidateMounts() {
1985 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001986 std::lock_guard<std::recursive_mutex> lock(mQuotaDevicesLock);
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001987
1988 mQuotaDevices.clear();
1989
1990 std::ifstream in("/proc/mounts");
1991 if (!in.is_open()) {
1992 return error("Failed to read mounts");
1993 }
1994
1995 std::string source;
1996 std::string target;
1997 std::string ignored;
1998 struct dqblk dq;
1999 while (!in.eof()) {
2000 std::getline(in, source, ' ');
2001 std::getline(in, target, ' ');
2002 std::getline(in, ignored);
2003
2004 if (source.compare(0, 11, "/dev/block/") == 0) {
2005 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0,
2006 reinterpret_cast<char*>(&dq)) == 0) {
2007 LOG(DEBUG) << "Found " << source << " with quota";
2008 mQuotaDevices[target] = source;
2009 }
2010 }
2011 }
2012 return ok();
2013}
2014
2015std::string InstalldNativeService::findQuotaDeviceForUuid(
2016 const std::unique_ptr<std::string>& uuid) {
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07002017 std::lock_guard<std::recursive_mutex> lock(mQuotaDevicesLock);
Jeff Sharkey66b1a122017-01-16 20:57:45 -07002018 auto path = create_data_path(uuid ? uuid->c_str() : nullptr);
2019 return mQuotaDevices[path];
2020}
2021
Jeff Sharkey325b8c92017-02-21 10:00:53 -07002022binder::Status InstalldNativeService::isQuotaSupported(
2023 const std::unique_ptr<std::string>& volumeUuid, bool* _aidl_return) {
2024 *_aidl_return = !findQuotaDeviceForUuid(volumeUuid).empty();
2025 return ok();
2026}
2027
Andreas Gampe02d0de52015-11-11 20:43:16 -08002028} // namespace installd
2029} // namespace android