blob: e486589cda436a6830836806d8376671b5810586 [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)) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700362 const std::string profile_path = create_data_user_profile_package_path(userId, pkgname);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000363 // read-write-execute only for the app user.
364 if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700365 return error("Failed to prepare " + profile_path);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000366 }
Calin Juravlebc56e7d2016-05-24 15:33:12 +0100367 std::string profile_file = create_primary_profile(profile_path);
368 // read-write only for the app user.
369 if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700370 return error("Failed to prepare " + profile_path);
Calin Juravlebc56e7d2016-05-24 15:33:12 +0100371 }
Calin Juravle6a1648e2016-02-01 12:12:16 +0000372 const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname);
373 // dex2oat/profman runs under the shared app gid and it needs to read/write reference
374 // profiles.
Jeff Sharkeyadddd982017-01-03 14:33:50 -0700375 int shared_app_gid = multiuser_get_shared_gid(0, appId);
Jeff Sharkey2e6dc9b2016-12-15 14:50:11 -0700376 if ((shared_app_gid != -1) && fs_prepare_dir_strict(
Calin Juravle6a1648e2016-02-01 12:12:16 +0000377 ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700378 return error("Failed to prepare " + ref_profile_path);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000379 }
380 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700381 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700382 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700383}
384
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700385binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid,
386 const std::string& packageName, int32_t userId, int32_t flags) {
387 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700388 CHECK_ARGUMENT_UUID(uuid);
389 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700390 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700391
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700392 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
393 const char* pkgname = packageName.c_str();
394
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700395 // This method only exists to upgrade system apps that have requested
396 // forceDeviceEncrypted, so their default storage always lives in a
397 // consistent location. This only works on non-FBE devices, since we
398 // never want to risk exposing data on a device with real CE/DE storage.
399
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700400 auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname);
401 auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700402
403 // If neither directory is marked as default, assume CE is default
404 if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1
405 && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) {
406 if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700407 return error("Failed to mark default storage " + ce_path);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700408 }
409 }
410
411 // Migrate default data location if needed
412 auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path;
413 auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path;
414
415 if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) {
416 LOG(WARNING) << "Requested default storage " << target
417 << " is not active; migrating from " << source;
418 if (delete_dir_contents_and_dir(target) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700419 return error("Failed to delete " + target);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700420 }
421 if (rename(source.c_str(), target.c_str()) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700422 return error("Failed to rename " + source + " to " + target);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700423 }
424 }
425
Jeff Sharkey423e7462016-12-09 18:18:43 -0700426 return ok();
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700427}
428
Calin Juravle6a1648e2016-02-01 12:12:16 +0000429
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700430binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) {
431 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700432 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700433 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700434
Jeff Sharkey423e7462016-12-09 18:18:43 -0700435 binder::Status res = ok();
Calin Juravle76268c52017-03-09 13:19:42 -0800436 if (!clear_reference_profile(packageName)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700437 res = error("Failed to clear reference profile for " + packageName);
438 }
Calin Juravle76268c52017-03-09 13:19:42 -0800439 if (!clear_current_profiles(packageName)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700440 res = error("Failed to clear current profiles for " + packageName);
441 }
442 return res;
Calin Juravle6a1648e2016-02-01 12:12:16 +0000443}
444
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700445binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid,
446 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
447 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700448 CHECK_ARGUMENT_UUID(uuid);
449 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700450 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700451
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700452 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
453 const char* pkgname = packageName.c_str();
454
Jeff Sharkey423e7462016-12-09 18:18:43 -0700455 binder::Status res = ok();
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700456 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700457 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
Jeff Sharkey1f6a7f12016-07-14 18:16:22 -0600458 if (flags & FLAG_CLEAR_CACHE_ONLY) {
459 path = read_path_inode(path, "cache", kXattrInodeCache);
460 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
461 path = read_path_inode(path, "code_cache", kXattrInodeCodeCache);
462 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700463 if (access(path.c_str(), F_OK) == 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700464 if (delete_dir_contents(path) != 0) {
465 res = error("Failed to delete contents of " + path);
466 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700467 }
468 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700469 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey1f6a7f12016-07-14 18:16:22 -0600470 std::string suffix = "";
471 bool only_cache = false;
472 if (flags & FLAG_CLEAR_CACHE_ONLY) {
473 suffix = CACHE_DIR_POSTFIX;
474 only_cache = true;
475 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
476 suffix = CODE_CACHE_DIR_POSTFIX;
477 only_cache = true;
478 }
479
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700480 auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix;
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700481 if (access(path.c_str(), F_OK) == 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700482 if (delete_dir_contents(path) != 0) {
483 res = error("Failed to delete contents of " + path);
484 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700485 }
Calin Juravleedae6692016-03-23 13:55:31 +0000486 if (!only_cache) {
Calin Juravle76268c52017-03-09 13:19:42 -0800487 if (!clear_current_profile(packageName, userId)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700488 res = error("Failed to clear current profile for " + packageName);
Calin Juravleedae6692016-03-23 13:55:31 +0000489 }
490 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700491 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700492 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700493}
494
Calin Juravle76268c52017-03-09 13:19:42 -0800495static int destroy_app_reference_profile(const std::string& pkgname) {
Calin Juravle7535e8e2016-03-29 16:05:40 +0100496 return delete_dir_contents_and_dir(
497 create_data_ref_profile_package_path(pkgname),
498 /*ignore_if_missing*/ true);
499}
500
Calin Juravle76268c52017-03-09 13:19:42 -0800501static int destroy_app_current_profiles(const std::string& pkgname, userid_t userid) {
Calin Juravleb06f98a2016-03-28 15:11:01 +0100502 return delete_dir_contents_and_dir(
503 create_data_user_profile_package_path(userid, pkgname),
504 /*ignore_if_missing*/ true);
Calin Juravleedae6692016-03-23 13:55:31 +0000505}
506
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700507binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) {
508 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700509 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700510 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700511
Jeff Sharkey423e7462016-12-09 18:18:43 -0700512 binder::Status res = ok();
Calin Juravleedae6692016-03-23 13:55:31 +0000513 std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
514 for (auto user : users) {
Calin Juravle76268c52017-03-09 13:19:42 -0800515 if (destroy_app_current_profiles(packageName, user) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700516 res = error("Failed to destroy current profiles for " + packageName);
517 }
Calin Juravleedae6692016-03-23 13:55:31 +0000518 }
Calin Juravle76268c52017-03-09 13:19:42 -0800519 if (destroy_app_reference_profile(packageName) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700520 res = error("Failed to destroy reference profile for " + packageName);
521 }
522 return res;
Calin Juravlecaa6b802016-03-22 14:15:52 +0000523}
524
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700525binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid,
526 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
527 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700528 CHECK_ARGUMENT_UUID(uuid);
529 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700530 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700531
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700532 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
533 const char* pkgname = packageName.c_str();
534
Jeff Sharkey423e7462016-12-09 18:18:43 -0700535 binder::Status res = ok();
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700536 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700537 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
538 if (delete_dir_contents_and_dir(path) != 0) {
539 res = error("Failed to delete " + path);
540 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700541 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700542 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700543 auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
544 if (delete_dir_contents_and_dir(path) != 0) {
545 res = error("Failed to delete " + path);
546 }
Calin Juravle76268c52017-03-09 13:19:42 -0800547 destroy_app_current_profiles(packageName, userId);
Calin Juravle7535e8e2016-03-29 16:05:40 +0100548 // TODO(calin): If the package is still installed by other users it's probably
549 // beneficial to keep the reference profile around.
550 // Verify if it's ok to do that.
Calin Juravle76268c52017-03-09 13:19:42 -0800551 destroy_app_reference_profile(packageName);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700552 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700553 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700554}
555
Jeff Sharkey0274c972016-12-06 09:32:04 -0700556binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid,
557 const std::unique_ptr<std::string>& toUuid, const std::string& packageName,
558 const std::string& dataAppName, int32_t appId, const std::string& seInfo,
559 int32_t targetSdkVersion) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700560 ENFORCE_UID(AID_SYSTEM);
561 CHECK_ARGUMENT_UUID(fromUuid);
562 CHECK_ARGUMENT_UUID(toUuid);
563 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700564 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700565
566 const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr;
567 const char* to_uuid = toUuid ? toUuid->c_str() : nullptr;
568 const char* package_name = packageName.c_str();
569 const char* data_app_name = dataAppName.c_str();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700570
Jeff Sharkey423e7462016-12-09 18:18:43 -0700571 binder::Status res = ok();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700572 std::vector<userid_t> users = get_known_users(from_uuid);
573
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700574 // Copy app
575 {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600576 auto from = create_data_app_package_path(from_uuid, data_app_name);
577 auto to = create_data_app_package_path(to_uuid, data_app_name);
578 auto to_parent = create_data_app_path(to_uuid);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700579
580 char *argv[] = {
581 (char*) kCpPath,
582 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
583 (char*) "-p", /* preserve timestamps, ownership, and permissions */
584 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
585 (char*) "-P", /* Do not follow symlinks [default] */
586 (char*) "-d", /* don't dereference symlinks */
587 (char*) from.c_str(),
588 (char*) to_parent.c_str()
589 };
590
591 LOG(DEBUG) << "Copying " << from << " to " << to;
592 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700593 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700594 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700595 goto fail;
596 }
597
598 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700599 res = error("Failed to restorecon " + to);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700600 goto fail;
601 }
602 }
603
604 // Copy private data for all known users
Jeff Sharkeye3637242015-04-08 20:56:42 -0700605 for (auto user : users) {
Jeff Sharkeye3637242015-04-08 20:56:42 -0700606
607 // Data source may not exist for all users; that's okay
Jeff Sharkey51c94492016-05-10 17:46:39 -0600608 auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name);
609 if (access(from_ce.c_str(), F_OK) != 0) {
610 LOG(INFO) << "Missing source " << from_ce;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700611 continue;
612 }
613
Jeff Sharkey0274c972016-12-06 09:32:04 -0700614 if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId,
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700615 seInfo, targetSdkVersion, nullptr).isOk()) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700616 res = error("Failed to create package target");
Jeff Sharkeye3637242015-04-08 20:56:42 -0700617 goto fail;
618 }
619
620 char *argv[] = {
621 (char*) kCpPath,
622 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
623 (char*) "-p", /* preserve timestamps, ownership, and permissions */
624 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
625 (char*) "-P", /* Do not follow symlinks [default] */
626 (char*) "-d", /* don't dereference symlinks */
Jeff Sharkey51c94492016-05-10 17:46:39 -0600627 nullptr,
628 nullptr
Jeff Sharkeye3637242015-04-08 20:56:42 -0700629 };
630
Jeff Sharkey51c94492016-05-10 17:46:39 -0600631 {
632 auto from = create_data_user_de_package_path(from_uuid, user, package_name);
633 auto to = create_data_user_de_path(to_uuid, user);
634 argv[6] = (char*) from.c_str();
635 argv[7] = (char*) to.c_str();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700636
Jeff Sharkey51c94492016-05-10 17:46:39 -0600637 LOG(DEBUG) << "Copying " << from << " to " << to;
638 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
639 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700640 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkey51c94492016-05-10 17:46:39 -0600641 goto fail;
642 }
643 }
644 {
645 auto from = create_data_user_ce_package_path(from_uuid, user, package_name);
646 auto to = create_data_user_ce_path(to_uuid, user);
647 argv[6] = (char*) from.c_str();
648 argv[7] = (char*) to.c_str();
649
650 LOG(DEBUG) << "Copying " << from << " to " << to;
651 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
652 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700653 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkey51c94492016-05-10 17:46:39 -0600654 goto fail;
655 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700656 }
657
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700658 if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE,
659 appId, seInfo).isOk()) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700660 res = error("Failed to restorecon");
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700661 goto fail;
662 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700663 }
664
Jeff Sharkey31f08982015-07-07 13:31:37 -0700665 // We let the framework scan the new location and persist that before
666 // deleting the data in the old location; this ordering ensures that
667 // we can recover from things like battery pulls.
Jeff Sharkey423e7462016-12-09 18:18:43 -0700668 return ok();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700669
670fail:
671 // Nuke everything we might have already copied
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700672 {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600673 auto to = create_data_app_package_path(to_uuid, data_app_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700674 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
675 LOG(WARNING) << "Failed to rollback " << to;
676 }
677 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700678 for (auto user : users) {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600679 {
680 auto to = create_data_user_de_package_path(to_uuid, user, package_name);
681 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
682 LOG(WARNING) << "Failed to rollback " << to;
683 }
684 }
685 {
686 auto to = create_data_user_ce_package_path(to_uuid, user, package_name);
687 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
688 LOG(WARNING) << "Failed to rollback " << to;
689 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700690 }
691 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700692 return res;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700693}
694
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700695binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid,
696 int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) {
697 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700698 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700699 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700700
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700701 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600702 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700703 if (uuid_ == nullptr) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700704 if (ensure_config_user_dirs(userId) != 0) {
Jeff Sharkey7be5ead2016-12-12 13:18:46 -0700705 return error(StringPrintf("Failed to ensure dirs for %d", userId));
Jeff Sharkey423e7462016-12-09 18:18:43 -0700706 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700707 }
708 }
Jeff Sharkey7be5ead2016-12-12 13:18:46 -0700709 return ok();
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700710}
711
712binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid,
713 int32_t userId, int32_t flags) {
714 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700715 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700716 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700717
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700718 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey423e7462016-12-09 18:18:43 -0700719 binder::Status res = ok();
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700720 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700721 auto path = create_data_user_de_path(uuid_, userId);
722 if (delete_dir_contents_and_dir(path, true) != 0) {
723 res = error("Failed to delete " + path);
724 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700725 if (uuid_ == nullptr) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700726 path = create_data_misc_legacy_path(userId);
727 if (delete_dir_contents_and_dir(path, true) != 0) {
728 res = error("Failed to delete " + path);
729 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700730 path = create_data_user_profile_path(userId);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700731 if (delete_dir_contents_and_dir(path, true) != 0) {
732 res = error("Failed to delete " + path);
733 }
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700734 }
Robin Lee095c7632014-04-25 15:05:19 +0100735 }
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600736 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700737 auto path = create_data_user_ce_path(uuid_, userId);
738 if (delete_dir_contents_and_dir(path, true) != 0) {
739 res = error("Failed to delete " + path);
740 }
741 path = create_data_media_path(uuid_, userId);
742 if (delete_dir_contents_and_dir(path, true) != 0) {
743 res = error("Failed to delete " + path);
744 }
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600745 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700746 return res;
Robin Lee095c7632014-04-25 15:05:19 +0100747}
748
Mike Lockwood94afecf2012-10-24 10:45:23 -0700749/* Try to ensure free_size bytes of storage are available.
750 * Returns 0 on success.
751 * This is rather simple-minded because doing a full LRU would
752 * be potentially memory-intensive, and without atime it would
753 * also require that apps constantly modify file metadata even
754 * when just reading from the cache, which is pretty awful.
755 */
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700756binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid,
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700757 int64_t freeStorageSize, int32_t flags) {
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700758 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700759 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700760 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700761
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700762 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700763 auto data_path = create_data_path(uuid_);
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700764 auto device = findQuotaDeviceForUuid(uuid);
765 auto noop = (flags & FLAG_FREE_CACHE_NOOP);
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700766
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700767 int64_t free = data_disk_free(data_path);
768 int64_t needed = freeStorageSize - free;
769 if (free < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700770 return error("Failed to determine free space for " + data_path);
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700771 } else if (free >= freeStorageSize) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700772 return ok();
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700773 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700774
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700775 LOG(DEBUG) << "Found " << data_path << " with " << free << " free; caller requested "
776 << freeStorageSize;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700777
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700778 if (flags & FLAG_FREE_CACHE_V2) {
779 // This new cache strategy fairly removes files from UIDs by deleting
780 // files from the UIDs which are most over their allocated quota
781
782 // 1. Create trackers for every known UID
783 ATRACE_BEGIN("create");
784 std::unordered_map<uid_t, std::shared_ptr<CacheTracker>> trackers;
785 for (auto user : get_known_users(uuid_)) {
786 FTS *fts;
787 FTSENT *p;
788 char *argv[] = {
789 (char*) create_data_user_ce_path(uuid_, user).c_str(),
790 (char*) create_data_user_de_path(uuid_, user).c_str(),
791 nullptr
792 };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700793 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700794 return error("Failed to fts_open");
795 }
796 while ((p = fts_read(fts)) != NULL) {
797 if (p->fts_info == FTS_D && p->fts_level == 1) {
798 uid_t uid = p->fts_statp->st_uid;
799 auto search = trackers.find(uid);
800 if (search != trackers.end()) {
801 search->second->addDataPath(p->fts_path);
802 } else {
803 auto tracker = std::shared_ptr<CacheTracker>(new CacheTracker(
804 multiuser_get_user_id(uid), multiuser_get_app_id(uid), device));
805 tracker->addDataPath(p->fts_path);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700806 {
807 std::lock_guard<std::recursive_mutex> lock(mCacheQuotasLock);
808 tracker->cacheQuota = mCacheQuotas[uid];
809 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700810 if (tracker->cacheQuota == 0) {
811 LOG(WARNING) << "UID " << uid << " has no cache quota; assuming 64MB";
812 tracker->cacheQuota = 67108864;
813 }
814 trackers[uid] = tracker;
815 }
816 fts_set(fts, p, FTS_SKIP);
817 }
818 }
819 fts_close(fts);
820 }
821 ATRACE_END();
822
823 // 2. Populate tracker stats and insert into priority queue
824 ATRACE_BEGIN("populate");
825 auto cmp = [](std::shared_ptr<CacheTracker> left, std::shared_ptr<CacheTracker> right) {
826 return (left->getCacheRatio() < right->getCacheRatio());
827 };
828 std::priority_queue<std::shared_ptr<CacheTracker>,
829 std::vector<std::shared_ptr<CacheTracker>>, decltype(cmp)> queue(cmp);
830 for (const auto& it : trackers) {
831 it.second->loadStats();
832 queue.push(it.second);
833 }
834 ATRACE_END();
835
836 // 3. Bounce across the queue, freeing items from whichever tracker is
837 // the most over their assigned quota
838 ATRACE_BEGIN("bounce");
839 std::shared_ptr<CacheTracker> active;
840 while (active || !queue.empty()) {
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700841 // Only look at apps under quota when explicitly requested
842 if (active && (active->getCacheRatio() < 10000)
843 && !(flags & FLAG_FREE_CACHE_V2_DEFY_QUOTA)) {
844 LOG(DEBUG) << "Active ratio " << active->getCacheRatio()
845 << " isn't over quota, and defy not requested";
846 break;
847 }
848
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700849 // Find the best tracker to work with; this might involve swapping
850 // if the active tracker is no longer the most over quota
851 bool nextBetter = active && !queue.empty()
852 && active->getCacheRatio() < queue.top()->getCacheRatio();
853 if (!active || nextBetter) {
854 if (active) {
855 // Current tracker still has items, so we'll consider it
856 // again later once it bubbles up to surface
857 queue.push(active);
858 }
859 active = queue.top(); queue.pop();
860 active->ensureItems();
861 continue;
862 }
863
864 // If no items remain, go find another tracker
865 if (active->items.empty()) {
866 active = nullptr;
867 continue;
868 } else {
869 auto item = active->items.back();
870 active->items.pop_back();
871
872 LOG(DEBUG) << "Purging " << item->toString() << " from " << active->toString();
873 if (!noop) {
874 item->purge();
875 }
876 active->cacheUsed -= item->size;
877 needed -= item->size;
878 }
879
880 // Verify that we're actually done before bailing, since sneaky
881 // apps might be using hardlinks
882 if (needed <= 0) {
883 free = data_disk_free(data_path);
884 needed = freeStorageSize - free;
885 if (needed <= 0) {
886 break;
887 } else {
888 LOG(WARNING) << "Expected to be done but still need " << needed;
889 }
890 }
891 }
892 ATRACE_END();
893
894 } else {
895 ATRACE_BEGIN("start");
896 cache_t* cache = start_cache_collection();
897 ATRACE_END();
898
899 ATRACE_BEGIN("add");
900 for (auto user : get_known_users(uuid_)) {
901 add_cache_files(cache, create_data_user_ce_path(uuid_, user));
902 add_cache_files(cache, create_data_user_de_path(uuid_, user));
903 add_cache_files(cache,
904 StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str()));
905 }
Fyodor Kupolovf3124d82016-09-07 13:07:45 -0700906 // Add files from /data/preloads/file_cache
907 if (uuid == nullptr) {
908 add_preloads_file_cache(cache, uuid_);
909 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700910 ATRACE_END();
911
912 ATRACE_BEGIN("clear");
913 clear_cache_files(data_path, cache, freeStorageSize);
914 ATRACE_END();
915
916 ATRACE_BEGIN("finish");
917 finish_cache_collection(cache);
918 ATRACE_END();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700919 }
920
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700921 free = data_disk_free(data_path);
922 if (free >= freeStorageSize) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700923 return ok();
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700924 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700925 return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64,
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700926 freeStorageSize, data_path.c_str(), free));
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700927 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700928}
929
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700930binder::Status InstalldNativeService::rmdex(const std::string& codePath,
931 const std::string& instructionSet) {
932 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700933 std::lock_guard<std::recursive_mutex> lock(mLock);
934
Mike Lockwood94afecf2012-10-24 10:45:23 -0700935 char dex_path[PKG_PATH_MAX];
936
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700937 const char* path = codePath.c_str();
938 const char* instruction_set = instructionSet.c_str();
939
Jeff Sharkey770180a2014-09-08 17:14:26 -0700940 if (validate_apk_path(path) && validate_system_app_path(path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700941 return error("Invalid path " + codePath);
Jeff Sharkey770180a2014-09-08 17:14:26 -0700942 }
943
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700944 if (!create_cache_path(dex_path, path, instruction_set)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700945 return error("Failed to create cache path for " + codePath);
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700946 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700947
948 ALOGV("unlink %s\n", dex_path);
949 if (unlink(dex_path) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700950 return error(StringPrintf("Failed to unlink %s", dex_path));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700951 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700952 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700953 }
954}
955
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700956struct stats {
957 int64_t codeSize;
958 int64_t dataSize;
959 int64_t cacheSize;
960};
961
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700962#if MEASURE_DEBUG
963static std::string toString(std::vector<int64_t> values) {
964 std::stringstream res;
965 res << "[";
966 for (size_t i = 0; i < values.size(); i++) {
967 res << values[i];
968 if (i < values.size() - 1) {
969 res << ",";
970 }
971 }
972 res << "]";
973 return res.str();
974}
975#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700976
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700977static void collectQuotaStats(const std::string& device, int32_t userId,
Jeff Sharkey7bf5b952017-01-19 09:21:36 -0700978 int32_t appId, struct stats* stats, struct stats* extStats) {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700979 if (device.empty()) return;
980
981 struct dqblk dq;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700982
983 uid_t uid = multiuser_get_uid(userId, appId);
984 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
985 reinterpret_cast<char*>(&dq)) != 0) {
986 if (errno != ESRCH) {
987 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
988 }
989 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700990#if MEASURE_DEBUG
991 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
992#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700993 stats->dataSize += dq.dqb_curspace;
994 }
995
996 int cacheGid = multiuser_get_cache_gid(userId, appId);
997 if (cacheGid != -1) {
998 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid,
999 reinterpret_cast<char*>(&dq)) != 0) {
1000 if (errno != ESRCH) {
1001 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid;
1002 }
1003 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001004#if MEASURE_DEBUG
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001005 LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001006#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001007 stats->cacheSize += dq.dqb_curspace;
1008 }
1009 }
1010
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001011 int extGid = multiuser_get_ext_gid(userId, appId);
1012 if (extGid != -1) {
1013 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), extGid,
1014 reinterpret_cast<char*>(&dq)) != 0) {
1015 if (errno != ESRCH) {
1016 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << extGid;
1017 }
1018 } else {
1019#if MEASURE_DEBUG
1020 LOG(DEBUG) << "quotactl() for GID " << extGid << " " << dq.dqb_curspace;
1021#endif
1022 extStats->dataSize += dq.dqb_curspace;
1023 }
1024 }
1025
1026 int sharedGid = multiuser_get_shared_gid(userId, appId);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001027 if (sharedGid != -1) {
1028 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid,
1029 reinterpret_cast<char*>(&dq)) != 0) {
1030 if (errno != ESRCH) {
1031 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid;
1032 }
1033 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001034#if MEASURE_DEBUG
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001035 LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001036#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001037 stats->codeSize += dq.dqb_curspace;
1038 }
1039 }
1040}
1041
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001042static void collectManualStats(const std::string& path, struct stats* stats) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001043 DIR *d;
1044 int dfd;
1045 struct dirent *de;
1046 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001047
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001048 d = opendir(path.c_str());
1049 if (d == nullptr) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001050 if (errno != ENOENT) {
1051 PLOG(WARNING) << "Failed to open " << path;
1052 }
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001053 return;
1054 }
1055 dfd = dirfd(d);
1056 while ((de = readdir(d))) {
1057 const char *name = de->d_name;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001058
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001059 int64_t size = 0;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001060 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001061 size = s.st_blocks * 512;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001062 }
1063
1064 if (de->d_type == DT_DIR) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001065 if (!strcmp(name, ".")) {
1066 // Don't recurse, but still count node size
1067 } else if (!strcmp(name, "..")) {
1068 // Don't recurse or count node size
1069 continue;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001070 } else {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001071 // Measure all children nodes
1072 size = 0;
1073 calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001074 }
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001075
1076 if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) {
1077 stats->cacheSize += size;
1078 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001079 }
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001080
1081 // Legacy symlink isn't owned by app
1082 if (de->d_type == DT_LNK && !strcmp(name, "lib")) {
1083 continue;
1084 }
1085
1086 // Everything found inside is considered data
1087 stats->dataSize += size;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001088 }
1089 closedir(d);
1090}
1091
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001092static void collectManualStatsForUser(const std::string& path, struct stats* stats,
1093 bool exclude_apps = false) {
1094 DIR *d;
1095 int dfd;
1096 struct dirent *de;
1097 struct stat s;
1098
1099 d = opendir(path.c_str());
1100 if (d == nullptr) {
1101 if (errno != ENOENT) {
1102 PLOG(WARNING) << "Failed to open " << path;
1103 }
1104 return;
1105 }
1106 dfd = dirfd(d);
1107 while ((de = readdir(d))) {
1108 if (de->d_type == DT_DIR) {
1109 const char *name = de->d_name;
1110 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) {
1111 continue;
1112 }
1113 if (!strcmp(name, ".") || !strcmp(name, "..")) {
1114 continue;
1115 } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) {
1116 continue;
1117 } else {
1118 collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats);
1119 }
1120 }
1121 }
1122 closedir(d);
1123}
1124
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001125static void collectManualExternalStatsForUser(const std::string& path, struct stats* stats) {
1126 FTS *fts;
1127 FTSENT *p;
1128 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001129 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001130 PLOG(ERROR) << "Failed to fts_open " << path;
1131 return;
1132 }
1133 while ((p = fts_read(fts)) != NULL) {
Jeff Sharkey76ddaeb2017-01-25 22:15:42 -07001134 p->fts_number = p->fts_parent->fts_number;
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001135 switch (p->fts_info) {
1136 case FTS_D:
1137 if (p->fts_level == 4
1138 && !strcmp(p->fts_name, "cache")
1139 && !strcmp(p->fts_parent->fts_parent->fts_name, "data")
1140 && !strcmp(p->fts_parent->fts_parent->fts_parent->fts_name, "Android")) {
1141 p->fts_number = 1;
1142 }
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001143 // Fall through to count the directory
1144 case FTS_DEFAULT:
1145 case FTS_F:
1146 case FTS_SL:
1147 case FTS_SLNONE:
1148 int64_t size = (p->fts_statp->st_blocks * 512);
1149 if (p->fts_number == 1) {
1150 stats->cacheSize += size;
1151 }
1152 stats->dataSize += size;
1153 break;
1154 }
1155 }
1156 fts_close(fts);
1157}
1158
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001159binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001160 const std::vector<std::string>& packageNames, int32_t userId, int32_t flags,
1161 int32_t appId, const std::vector<int64_t>& ceDataInodes,
1162 const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) {
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001163 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001164 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001165 for (auto packageName : packageNames) {
1166 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
1167 }
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001168 // NOTE: Locking is relaxed on this method, since it's limited to
1169 // read-only measurements without mutation.
Jeff Sharkey423e7462016-12-09 18:18:43 -07001170
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001171 // When modifying this logic, always verify using tests:
1172 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize
1173
1174#if MEASURE_DEBUG
1175 LOG(INFO) << "Measuring user " << userId << " app " << appId;
1176#endif
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001177
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001178 // Here's a summary of the common storage locations across the platform,
1179 // and how they're each tagged:
1180 //
1181 // /data/app/com.example UID system
1182 // /data/app/com.example/oat UID system
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001183 // /data/user/0/com.example UID u0_a10 GID u0_a10
1184 // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache
1185 // /data/media/0/foo.txt UID u0_media_rw
1186 // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image
1187 // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext
1188 // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache
1189 // /data/media/obb/com.example UID system
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001190
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001191 struct stats stats;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001192 struct stats extStats;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001193 memset(&stats, 0, sizeof(stats));
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001194 memset(&extStats, 0, sizeof(extStats));
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001195
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001196 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001197
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001198 auto device = findQuotaDeviceForUuid(uuid);
1199 if (device.empty()) {
1200 flags &= ~FLAG_USE_QUOTA;
1201 }
1202
Jeff Sharkey466459b2017-01-17 15:25:01 -07001203 ATRACE_BEGIN("obb");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001204 for (auto packageName : packageNames) {
1205 auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str());
1206 calculate_tree_size(obbCodePath, &extStats.codeSize);
1207 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001208 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001209
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001210 if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001211 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001212 for (auto codePath : codePaths) {
1213 calculate_tree_size(codePath, &stats.codeSize, -1,
1214 multiuser_get_shared_gid(userId, appId));
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001215 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001216 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001217
Jeff Sharkey466459b2017-01-17 15:25:01 -07001218 ATRACE_BEGIN("quota");
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001219 collectQuotaStats(device, userId, appId, &stats, &extStats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001220 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001221
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001222 } else {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001223 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001224 for (auto codePath : codePaths) {
1225 calculate_tree_size(codePath, &stats.codeSize);
1226 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001227 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001228
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001229 for (size_t i = 0; i < packageNames.size(); i++) {
1230 const char* pkgname = packageNames[i].c_str();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001231
Jeff Sharkey466459b2017-01-17 15:25:01 -07001232 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001233 auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]);
1234 collectManualStats(cePath, &stats);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001235 auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname);
1236 collectManualStats(dePath, &stats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001237 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001238
Jeff Sharkey466459b2017-01-17 15:25:01 -07001239 ATRACE_BEGIN("profiles");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001240 auto userProfilePath = create_data_user_profile_package_path(userId, pkgname);
1241 calculate_tree_size(userProfilePath, &stats.dataSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001242 auto refProfilePath = create_data_ref_profile_package_path(pkgname);
1243 calculate_tree_size(refProfilePath, &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001244 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001245
Jeff Sharkey466459b2017-01-17 15:25:01 -07001246 ATRACE_BEGIN("external");
Jeff Sharkey76ddaeb2017-01-25 22:15:42 -07001247 auto extPath = create_data_media_package_path(uuid_, userId, "data", pkgname);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001248 collectManualStats(extPath, &extStats);
Jeff Sharkey76ddaeb2017-01-25 22:15:42 -07001249 auto mediaPath = create_data_media_package_path(uuid_, userId, "media", pkgname);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001250 calculate_tree_size(mediaPath, &extStats.dataSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001251 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001252 }
1253
Jeff Sharkey466459b2017-01-17 15:25:01 -07001254 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001255 int32_t sharedGid = multiuser_get_shared_gid(userId, appId);
1256 if (sharedGid != -1) {
1257 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize,
1258 sharedGid, -1);
1259 }
Calin Juravle35c84652017-03-03 17:00:35 -08001260 calculate_tree_size(create_data_user_profile_path(userId), &stats.dataSize,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001261 multiuser_get_uid(userId, appId), -1);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001262 ATRACE_END();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001263 }
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001264
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001265 std::vector<int64_t> ret;
1266 ret.push_back(stats.codeSize);
1267 ret.push_back(stats.dataSize);
1268 ret.push_back(stats.cacheSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001269 ret.push_back(extStats.codeSize);
1270 ret.push_back(extStats.dataSize);
1271 ret.push_back(extStats.cacheSize);
1272#if MEASURE_DEBUG
1273 LOG(DEBUG) << "Final result " << toString(ret);
1274#endif
1275 *_aidl_return = ret;
1276 return ok();
1277}
1278
1279binder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid,
1280 int32_t userId, int32_t flags, const std::vector<int32_t>& appIds,
1281 std::vector<int64_t>* _aidl_return) {
1282 ENFORCE_UID(AID_SYSTEM);
1283 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001284 // NOTE: Locking is relaxed on this method, since it's limited to
1285 // read-only measurements without mutation.
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001286
1287 // When modifying this logic, always verify using tests:
1288 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize
1289
1290#if MEASURE_DEBUG
1291 LOG(INFO) << "Measuring user " << userId;
1292#endif
1293
1294 struct stats stats;
1295 struct stats extStats;
1296 memset(&stats, 0, sizeof(stats));
1297 memset(&extStats, 0, sizeof(extStats));
1298
1299 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1300
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001301 auto device = findQuotaDeviceForUuid(uuid);
1302 if (device.empty()) {
1303 flags &= ~FLAG_USE_QUOTA;
1304 }
1305
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001306 if (flags & FLAG_USE_QUOTA) {
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001307 struct dqblk dq;
1308
1309 ATRACE_BEGIN("obb");
1310 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), AID_MEDIA_OBB,
1311 reinterpret_cast<char*>(&dq)) != 0) {
1312 if (errno != ESRCH) {
1313 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << AID_MEDIA_OBB;
1314 }
1315 } else {
1316#if MEASURE_DEBUG
1317 LOG(DEBUG) << "quotactl() for GID " << AID_MEDIA_OBB << " " << dq.dqb_curspace;
1318#endif
1319 extStats.codeSize += dq.dqb_curspace;
1320 }
1321 ATRACE_END();
1322
Jeff Sharkey466459b2017-01-17 15:25:01 -07001323 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001324 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001325 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001326
Jeff Sharkey466459b2017-01-17 15:25:01 -07001327 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001328 auto cePath = create_data_user_ce_path(uuid_, userId);
1329 collectManualStatsForUser(cePath, &stats, true);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001330 auto dePath = create_data_user_de_path(uuid_, userId);
1331 collectManualStatsForUser(dePath, &stats, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001332 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001333
Jeff Sharkey466459b2017-01-17 15:25:01 -07001334 ATRACE_BEGIN("profile");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001335 auto userProfilePath = create_data_user_profile_path(userId);
1336 calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001337 auto refProfilePath = create_data_ref_profile_path();
1338 calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001339 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001340
Jeff Sharkey466459b2017-01-17 15:25:01 -07001341 ATRACE_BEGIN("external");
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001342 uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW);
1343 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
1344 reinterpret_cast<char*>(&dq)) != 0) {
1345 if (errno != ESRCH) {
1346 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
1347 }
1348 } else {
1349#if MEASURE_DEBUG
1350 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001351#endif
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001352 extStats.dataSize += dq.dqb_curspace;
1353 }
1354 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001355
Jeff Sharkey466459b2017-01-17 15:25:01 -07001356 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001357 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize,
1358 -1, -1, true);
Calin Juravle35c84652017-03-03 17:00:35 -08001359 calculate_tree_size(create_data_user_profile_path(userId), &stats.dataSize,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001360 -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001361 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001362
Jeff Sharkey466459b2017-01-17 15:25:01 -07001363 ATRACE_BEGIN("quota");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001364 for (auto appId : appIds) {
1365 if (appId >= AID_APP_START) {
1366 collectQuotaStats(device, userId, appId, &stats, &extStats);
1367#if MEASURE_DEBUG
1368 // Sleep to make sure we don't lose logs
1369 usleep(1);
1370#endif
1371 }
1372 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001373 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001374 } else {
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001375 ATRACE_BEGIN("obb");
1376 auto obbPath = create_data_path(uuid_) + "/media/obb";
1377 calculate_tree_size(obbPath, &extStats.codeSize);
1378 ATRACE_END();
1379
Jeff Sharkey466459b2017-01-17 15:25:01 -07001380 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001381 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001382 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001383
Jeff Sharkey466459b2017-01-17 15:25:01 -07001384 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001385 auto cePath = create_data_user_ce_path(uuid_, userId);
1386 collectManualStatsForUser(cePath, &stats);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001387 auto dePath = create_data_user_de_path(uuid_, userId);
1388 collectManualStatsForUser(dePath, &stats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001389 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001390
Jeff Sharkey466459b2017-01-17 15:25:01 -07001391 ATRACE_BEGIN("profile");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001392 auto userProfilePath = create_data_user_profile_path(userId);
1393 calculate_tree_size(userProfilePath, &stats.dataSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001394 auto refProfilePath = create_data_ref_profile_path();
1395 calculate_tree_size(refProfilePath, &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001396 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001397
Jeff Sharkey466459b2017-01-17 15:25:01 -07001398 ATRACE_BEGIN("external");
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001399 auto dataMediaPath = create_data_media_path(uuid_, userId);
1400 collectManualExternalStatsForUser(dataMediaPath, &extStats);
1401#if MEASURE_DEBUG
1402 LOG(DEBUG) << "Measured external data " << extStats.dataSize << " cache "
1403 << extStats.cacheSize;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001404#endif
Jeff Sharkey7bf5b952017-01-19 09:21:36 -07001405 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001406
Jeff Sharkey466459b2017-01-17 15:25:01 -07001407 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001408 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize);
Calin Juravle35c84652017-03-03 17:00:35 -08001409 calculate_tree_size(create_data_user_profile_path(userId), &stats.dataSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001410 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001411 }
1412
1413 std::vector<int64_t> ret;
1414 ret.push_back(stats.codeSize);
1415 ret.push_back(stats.dataSize);
1416 ret.push_back(stats.cacheSize);
1417 ret.push_back(extStats.codeSize);
1418 ret.push_back(extStats.dataSize);
1419 ret.push_back(extStats.cacheSize);
1420#if MEASURE_DEBUG
1421 LOG(DEBUG) << "Final result " << toString(ret);
1422#endif
1423 *_aidl_return = ret;
1424 return ok();
1425}
1426
1427binder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid,
1428 int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) {
1429 ENFORCE_UID(AID_SYSTEM);
1430 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001431 // NOTE: Locking is relaxed on this method, since it's limited to
1432 // read-only measurements without mutation.
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001433
1434 // When modifying this logic, always verify using tests:
1435 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize
1436
1437#if MEASURE_DEBUG
1438 LOG(INFO) << "Measuring external " << userId;
1439#endif
1440
1441 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1442
1443 int64_t totalSize = 0;
1444 int64_t audioSize = 0;
1445 int64_t videoSize = 0;
1446 int64_t imageSize = 0;
1447
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001448 auto device = findQuotaDeviceForUuid(uuid);
1449 if (device.empty()) {
1450 flags &= ~FLAG_USE_QUOTA;
1451 }
1452
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001453 if (flags & FLAG_USE_QUOTA) {
1454 struct dqblk dq;
1455
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001456 uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW);
1457 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
1458 reinterpret_cast<char*>(&dq)) != 0) {
1459 if (errno != ESRCH) {
1460 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
1461 }
1462 } else {
1463#if MEASURE_DEBUG
1464 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
1465#endif
1466 totalSize = dq.dqb_curspace;
1467 }
1468
1469 gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO);
1470 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid,
1471 reinterpret_cast<char*>(&dq)) == 0) {
1472#if MEASURE_DEBUG
1473 LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace;
1474#endif
1475 audioSize = dq.dqb_curspace;
1476 }
1477 gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO);
1478 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid,
1479 reinterpret_cast<char*>(&dq)) == 0) {
1480#if MEASURE_DEBUG
1481 LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace;
1482#endif
1483 videoSize = dq.dqb_curspace;
1484 }
1485 gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE);
1486 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid,
1487 reinterpret_cast<char*>(&dq)) == 0) {
1488#if MEASURE_DEBUG
1489 LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace;
1490#endif
1491 imageSize = dq.dqb_curspace;
1492 }
1493 } else {
1494 FTS *fts;
1495 FTSENT *p;
1496 auto path = create_data_media_path(uuid_, userId);
1497 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001498 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001499 return error("Failed to fts_open " + path);
1500 }
1501 while ((p = fts_read(fts)) != NULL) {
1502 char* ext;
1503 int64_t size = (p->fts_statp->st_blocks * 512);
1504 switch (p->fts_info) {
1505 case FTS_F:
1506 // Only categorize files not belonging to apps
1507 if (p->fts_statp->st_gid < AID_APP_START) {
1508 ext = strrchr(p->fts_name, '.');
1509 if (ext != nullptr) {
1510 switch (MatchExtension(++ext)) {
1511 case AID_MEDIA_AUDIO: audioSize += size; break;
1512 case AID_MEDIA_VIDEO: videoSize += size; break;
1513 case AID_MEDIA_IMAGE: imageSize += size; break;
1514 }
1515 }
1516 }
1517 // Fall through to always count against total
1518 case FTS_D:
1519 case FTS_DEFAULT:
1520 case FTS_SL:
1521 case FTS_SLNONE:
1522 totalSize += size;
1523 break;
1524 }
1525 }
1526 fts_close(fts);
1527 }
1528
1529 std::vector<int64_t> ret;
1530 ret.push_back(totalSize);
1531 ret.push_back(audioSize);
1532 ret.push_back(videoSize);
1533 ret.push_back(imageSize);
1534#if MEASURE_DEBUG
1535 LOG(DEBUG) << "Final result " << toString(ret);
1536#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001537 *_aidl_return = ret;
Jeff Sharkey423e7462016-12-09 18:18:43 -07001538 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001539}
1540
Jeff Sharkey88ddd942017-01-17 18:05:54 -07001541binder::Status InstalldNativeService::setAppQuota(const std::unique_ptr<std::string>& uuid,
1542 int32_t userId, int32_t appId, int64_t cacheQuota) {
1543 ENFORCE_UID(AID_SYSTEM);
1544 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001545 std::lock_guard<std::recursive_mutex> lock(mCacheQuotasLock);
Jeff Sharkey88ddd942017-01-17 18:05:54 -07001546
1547 int32_t uid = multiuser_get_uid(userId, appId);
1548 mCacheQuotas[uid] = cacheQuota;
1549
1550 return ok();
1551}
1552
David Sehr6727c2d2016-05-17 16:06:22 -07001553// Dumps the contents of a profile file, using pkgname's dex files for pretty
1554// printing the result.
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001555binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName,
1556 const std::string& codePaths, bool* _aidl_return) {
1557 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001558 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001559 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001560
1561 const char* pkgname = packageName.c_str();
Jeff Sharkey90aff262016-12-12 14:28:24 -07001562 const char* code_paths = codePaths.c_str();
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001563
Jeff Sharkey90aff262016-12-12 14:28:24 -07001564 *_aidl_return = dump_profiles(uid, pkgname, code_paths);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001565 return ok();
David Sehr6727c2d2016-05-17 16:06:22 -07001566}
1567
Andreas Gampe4d0f8252016-03-20 11:30:28 -07001568// TODO: Consider returning error codes.
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001569binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName,
Jeff Sharkey423e7462016-12-09 18:18:43 -07001570 bool* _aidl_return) {
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001571 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001572 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001573 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001574
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001575 const char* pkgname = packageName.c_str();
1576 *_aidl_return = analyse_profiles(uid, pkgname);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001577 return ok();
Andreas Gampe4d0f8252016-03-20 11:30:28 -07001578}
1579
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001580binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid,
1581 const std::unique_ptr<std::string>& packageName, const std::string& instructionSet,
1582 int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags,
1583 const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid,
1584 const std::unique_ptr<std::string>& sharedLibraries) {
1585 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001586 CHECK_ARGUMENT_UUID(uuid);
1587 if (packageName && *packageName != "*") {
1588 CHECK_ARGUMENT_PACKAGE_NAME(*packageName);
1589 }
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001590 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001591
1592 const char* apk_path = apkPath.c_str();
1593 const char* pkgname = packageName ? packageName->c_str() : "*";
1594 const char* instruction_set = instructionSet.c_str();
1595 const char* oat_dir = outputPath ? outputPath->c_str() : nullptr;
1596 const char* compiler_filter = compilerFilter.c_str();
1597 const char* volume_uuid = uuid ? uuid->c_str() : nullptr;
1598 const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr;
1599
1600 int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded,
1601 oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001602 return res ? error(res, "Failed to dexopt") : ok();
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001603}
1604
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001605binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) {
1606 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001607 std::lock_guard<std::recursive_mutex> lock(mLock);
1608
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001609 const char* instruction_set = instructionSet.c_str();
1610
1611 char boot_marker_path[PKG_PATH_MAX];
1612 sprintf(boot_marker_path,
Andreas Gampe02d0de52015-11-11 20:43:16 -08001613 "%s/%s/%s/.booting",
1614 android_data_dir.path,
1615 DALVIK_CACHE,
1616 instruction_set);
Narayan Kamath091ea772014-11-10 15:03:46 +00001617
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001618 ALOGV("mark_boot_complete : %s", boot_marker_path);
1619 if (unlink(boot_marker_path) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001620 return error(StringPrintf("Failed to unlink %s", boot_marker_path));
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001621 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001622 return ok();
Narayan Kamath091ea772014-11-10 15:03:46 +00001623}
1624
Mike Lockwood94afecf2012-10-24 10:45:23 -07001625void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1626 struct stat* statbuf)
1627{
1628 while (path[basepos] != 0) {
1629 if (path[basepos] == '/') {
1630 path[basepos] = 0;
1631 if (lstat(path, statbuf) < 0) {
1632 ALOGV("Making directory: %s\n", path);
1633 if (mkdir(path, mode) == 0) {
1634 chown(path, uid, gid);
1635 } else {
1636 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1637 }
1638 }
1639 path[basepos] = '/';
1640 basepos++;
1641 }
1642 basepos++;
1643 }
1644}
1645
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001646binder::Status InstalldNativeService::linkNativeLibraryDirectory(
1647 const std::unique_ptr<std::string>& uuid, const std::string& packageName,
1648 const std::string& nativeLibPath32, int32_t userId) {
1649 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001650 CHECK_ARGUMENT_UUID(uuid);
1651 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001652 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001653
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001654 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1655 const char* pkgname = packageName.c_str();
1656 const char* asecLibDir = nativeLibPath32.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001657 struct stat s, libStat;
Jeff Sharkey423e7462016-12-09 18:18:43 -07001658 binder::Status res = ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001659
Jeff Sharkey423e7462016-12-09 18:18:43 -07001660 auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname);
1661 auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX;
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001662
1663 const char* pkgdir = _pkgdir.c_str();
1664 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001665
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001666 if (stat(pkgdir, &s) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001667 return error("Failed to stat " + _pkgdir);
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001668 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001669
1670 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001671 return error("Failed to chown " + _pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001672 }
1673
1674 if (chmod(pkgdir, 0700) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001675 res = error("Failed to chmod " + _pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001676 goto out;
1677 }
1678
1679 if (lstat(libsymlink, &libStat) < 0) {
1680 if (errno != ENOENT) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001681 res = error("Failed to stat " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001682 goto out;
1683 }
1684 } else {
1685 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001686 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001687 res = error("Failed to delete " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001688 goto out;
1689 }
1690 } else if (S_ISLNK(libStat.st_mode)) {
1691 if (unlink(libsymlink) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001692 res = error("Failed to unlink " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001693 goto out;
1694 }
1695 }
1696 }
1697
1698 if (symlink(asecLibDir, libsymlink) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001699 res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001700 goto out;
1701 }
1702
1703out:
1704 if (chmod(pkgdir, s.st_mode) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001705 auto msg = "Failed to cleanup chmod " + _pkgdir;
1706 if (res.isOk()) {
1707 res = error(msg);
1708 } else {
1709 PLOG(ERROR) << msg;
1710 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001711 }
1712
1713 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001714 auto msg = "Failed to cleanup chown " + _pkgdir;
1715 if (res.isOk()) {
1716 res = error(msg);
1717 } else {
1718 PLOG(ERROR) << msg;
1719 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001720 }
1721
Jeff Sharkey423e7462016-12-09 18:18:43 -07001722 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001723}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001724
1725static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1726{
1727 static const char *IDMAP_BIN = "/system/bin/idmap";
1728 static const size_t MAX_INT_LEN = 32;
1729 char idmap_str[MAX_INT_LEN];
1730
1731 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1732
1733 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1734 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1735}
1736
1737// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1738// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1739static int flatten_path(const char *prefix, const char *suffix,
1740 const char *overlay_path, char *idmap_path, size_t N)
1741{
1742 if (overlay_path == NULL || idmap_path == NULL) {
1743 return -1;
1744 }
1745 const size_t len_overlay_path = strlen(overlay_path);
1746 // will access overlay_path + 1 further below; requires absolute path
1747 if (len_overlay_path < 2 || *overlay_path != '/') {
1748 return -1;
1749 }
1750 const size_t len_idmap_root = strlen(prefix);
1751 const size_t len_suffix = strlen(suffix);
1752 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1753 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1754 // additions below would cause overflow
1755 return -1;
1756 }
1757 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1758 return -1;
1759 }
1760 memset(idmap_path, 0, N);
1761 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1762 char *ch = idmap_path + len_idmap_root;
1763 while (*ch != '\0') {
1764 if (*ch == '/') {
1765 *ch = '@';
1766 }
1767 ++ch;
1768 }
1769 return 0;
1770}
1771
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001772binder::Status InstalldNativeService::idmap(const std::string& targetApkPath,
1773 const std::string& overlayApkPath, int32_t uid) {
1774 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001775 std::lock_guard<std::recursive_mutex> lock(mLock);
1776
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001777 const char* target_apk = targetApkPath.c_str();
1778 const char* overlay_apk = overlayApkPath.c_str();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001779 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1780
1781 int idmap_fd = -1;
1782 char idmap_path[PATH_MAX];
1783
1784 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1785 idmap_path, sizeof(idmap_path)) == -1) {
1786 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1787 goto fail;
1788 }
1789
1790 unlink(idmap_path);
1791 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1792 if (idmap_fd < 0) {
1793 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1794 goto fail;
1795 }
1796 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1797 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1798 goto fail;
1799 }
1800 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1801 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1802 goto fail;
1803 }
1804
1805 pid_t pid;
1806 pid = fork();
1807 if (pid == 0) {
1808 /* child -- drop privileges before continuing */
1809 if (setgid(uid) != 0) {
1810 ALOGE("setgid(%d) failed during idmap\n", uid);
1811 exit(1);
1812 }
1813 if (setuid(uid) != 0) {
1814 ALOGE("setuid(%d) failed during idmap\n", uid);
1815 exit(1);
1816 }
1817 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1818 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1819 exit(1);
1820 }
1821
1822 run_idmap(target_apk, overlay_apk, idmap_fd);
1823 exit(1); /* only if exec call to idmap failed */
1824 } else {
1825 int status = wait_child(pid);
1826 if (status != 0) {
1827 ALOGE("idmap failed, status=0x%04x\n", status);
1828 goto fail;
1829 }
1830 }
1831
1832 close(idmap_fd);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001833 return ok();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001834fail:
1835 if (idmap_fd >= 0) {
1836 close(idmap_fd);
1837 unlink(idmap_path);
1838 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001839 return error();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001840}
Robert Craige9887e42014-02-20 10:25:56 -05001841
MÃ¥rten Kongstad5c6944c2015-12-15 14:02:30 +01001842binder::Status InstalldNativeService::removeIdmap(const std::string& overlayApkPath) {
1843 const char* overlay_apk = overlayApkPath.c_str();
1844 char idmap_path[PATH_MAX];
1845
1846 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1847 idmap_path, sizeof(idmap_path)) == -1) {
1848 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1849 return error();
1850 }
1851 if (unlink(idmap_path) < 0) {
1852 ALOGE("couldn't unlink idmap file %s\n", idmap_path);
1853 return error();
1854 }
1855 return ok();
1856}
1857
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001858binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid,
1859 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
1860 const std::string& seInfo) {
1861 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001862 CHECK_ARGUMENT_UUID(uuid);
1863 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001864 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001865
1866 binder::Status res = ok();
Robert Craige9887e42014-02-20 10:25:56 -05001867
Robert Craigda30dc72014-03-27 10:21:12 -04001868 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001869 unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE;
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001870 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1871 const char* pkgName = packageName.c_str();
1872 const char* seinfo = seInfo.c_str();
Robert Craigda30dc72014-03-27 10:21:12 -04001873
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001874 uid_t uid = multiuser_get_uid(userId, appId);
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -07001875 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001876 auto path = create_data_user_ce_package_path(uuid_, userId, pkgName);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001877 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001878 res = error("restorecon failed for " + path);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001879 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001880 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -07001881 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001882 auto path = create_data_user_de_package_path(uuid_, userId, pkgName);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001883 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001884 res = error("restorecon failed for " + path);
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001885 }
Robert Craige9887e42014-02-20 10:25:56 -05001886 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001887 return res;
Robert Craige9887e42014-02-20 10:25:56 -05001888}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001889
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001890binder::Status InstalldNativeService::createOatDir(const std::string& oatDir,
1891 const std::string& instructionSet) {
1892 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001893 std::lock_guard<std::recursive_mutex> lock(mLock);
1894
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001895 const char* oat_dir = oatDir.c_str();
1896 const char* instruction_set = instructionSet.c_str();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001897 char oat_instr_dir[PKG_PATH_MAX];
1898
1899 if (validate_apk_path(oat_dir)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001900 return error("Invalid path " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001901 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001902 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001903 return error("Failed to prepare " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001904 }
1905 if (selinux_android_restorecon(oat_dir, 0)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001906 return error("Failed to restorecon " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001907 }
1908 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001909 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001910 return error(StringPrintf("Failed to prepare %s", oat_instr_dir));
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001911 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001912 return ok();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001913}
1914
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001915binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) {
1916 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001917 std::lock_guard<std::recursive_mutex> lock(mLock);
1918
Jeff Sharkey423e7462016-12-09 18:18:43 -07001919 if (validate_apk_path(packageDir.c_str())) {
1920 return error("Invalid path " + packageDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001921 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001922 if (delete_dir_contents_and_dir(packageDir) != 0) {
1923 return error("Failed to delete " + packageDir);
1924 }
1925 return ok();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001926}
1927
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001928binder::Status InstalldNativeService::linkFile(const std::string& relativePath,
1929 const std::string& fromBase, const std::string& toBase) {
1930 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001931 std::lock_guard<std::recursive_mutex> lock(mLock);
1932
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001933 const char* relative_path = relativePath.c_str();
1934 const char* from_base = fromBase.c_str();
1935 const char* to_base = toBase.c_str();
Narayan Kamathd845c962015-06-04 13:20:27 +01001936 char from_path[PKG_PATH_MAX];
1937 char to_path[PKG_PATH_MAX];
1938 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
1939 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
1940
1941 if (validate_apk_path_subdirs(from_path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001942 return error(StringPrintf("Invalid from path %s", from_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001943 }
1944
1945 if (validate_apk_path_subdirs(to_path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001946 return error(StringPrintf("Invalid to path %s", to_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001947 }
1948
Jeff Sharkey423e7462016-12-09 18:18:43 -07001949 if (link(from_path, to_path) < 0) {
1950 return error(StringPrintf("Failed to link from %s to %s", from_path, to_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001951 }
1952
Jeff Sharkey423e7462016-12-09 18:18:43 -07001953 return ok();
Narayan Kamathd845c962015-06-04 13:20:27 +01001954}
1955
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001956binder::Status InstalldNativeService::moveAb(const std::string& apkPath,
1957 const std::string& instructionSet, const std::string& outputPath) {
1958 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001959 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001960
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001961 const char* apk_path = apkPath.c_str();
1962 const char* instruction_set = instructionSet.c_str();
1963 const char* oat_dir = outputPath.c_str();
Andreas Gampe0354bd02016-06-27 14:25:30 -07001964
Jeff Sharkey90aff262016-12-12 14:28:24 -07001965 bool success = move_ab(apk_path, instruction_set, oat_dir);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001966 return success ? ok() : error();
Andreas Gampee65b4fa2016-03-01 11:46:45 -08001967}
1968
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001969binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath,
1970 const std::string& instructionSet, const std::string& outputPath) {
1971 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001972 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001973
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001974 const char* apk_path = apkPath.c_str();
1975 const char* instruction_set = instructionSet.c_str();
1976 const char* oat_dir = outputPath.c_str();
1977
Jeff Sharkey90aff262016-12-12 14:28:24 -07001978 bool res = delete_odex(apk_path, instruction_set, oat_dir);
1979 return res ? ok() : error();
Andreas Gampe3964da02016-09-09 17:07:04 -07001980}
1981
Calin Juravlebd968362017-01-25 01:17:17 -08001982binder::Status InstalldNativeService::reconcileSecondaryDexFile(
1983 const std::string& dexPath, const std::string& packageName, int32_t uid,
1984 const std::vector<std::string>& isas, const std::unique_ptr<std::string>& volumeUuid,
1985 int32_t storage_flag, bool* _aidl_return) {
1986 ENFORCE_UID(AID_SYSTEM);
1987 CHECK_ARGUMENT_UUID(volumeUuid);
1988 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
1989
1990 std::lock_guard<std::recursive_mutex> lock(mLock);
1991 bool result = android::installd::reconcile_secondary_dex_file(
1992 dexPath, packageName, uid, isas, volumeUuid, storage_flag, _aidl_return);
1993 return result ? ok() : error();
1994}
1995
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001996binder::Status InstalldNativeService::invalidateMounts() {
1997 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001998 std::lock_guard<std::recursive_mutex> lock(mQuotaDevicesLock);
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001999
2000 mQuotaDevices.clear();
2001
2002 std::ifstream in("/proc/mounts");
2003 if (!in.is_open()) {
2004 return error("Failed to read mounts");
2005 }
2006
2007 std::string source;
2008 std::string target;
2009 std::string ignored;
2010 struct dqblk dq;
2011 while (!in.eof()) {
2012 std::getline(in, source, ' ');
2013 std::getline(in, target, ' ');
2014 std::getline(in, ignored);
2015
2016 if (source.compare(0, 11, "/dev/block/") == 0) {
2017 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0,
2018 reinterpret_cast<char*>(&dq)) == 0) {
2019 LOG(DEBUG) << "Found " << source << " with quota";
2020 mQuotaDevices[target] = source;
2021 }
2022 }
2023 }
2024 return ok();
2025}
2026
2027std::string InstalldNativeService::findQuotaDeviceForUuid(
2028 const std::unique_ptr<std::string>& uuid) {
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07002029 std::lock_guard<std::recursive_mutex> lock(mQuotaDevicesLock);
Jeff Sharkey66b1a122017-01-16 20:57:45 -07002030 auto path = create_data_path(uuid ? uuid->c_str() : nullptr);
2031 return mQuotaDevices[path];
2032}
2033
Jeff Sharkey325b8c92017-02-21 10:00:53 -07002034binder::Status InstalldNativeService::isQuotaSupported(
2035 const std::unique_ptr<std::string>& volumeUuid, bool* _aidl_return) {
2036 *_aidl_return = !findQuotaDeviceForUuid(volumeUuid).empty();
2037 return ok();
2038}
2039
Andreas Gampe02d0de52015-11-11 20:43:16 -08002040} // namespace installd
2041} // namespace android