blob: 03ee23f1a82f0c7e50282d723c503941b2efcc36 [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
Andreas Gampe02d0de52015-11-11 20:43:16 -080017#include "utils.h"
Mike Lockwood94afecf2012-10-24 10:45:23 -070018
Andreas Gampe02d0de52015-11-11 20:43:16 -080019#include <errno.h>
20#include <fcntl.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070021#include <fts.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080022#include <stdlib.h>
23#include <sys/stat.h>
24#include <sys/wait.h>
Jeff Sharkey9a998f42016-07-14 18:16:22 -060025#include <sys/xattr.h>
Jeff Sharkeyed909ae2017-03-22 21:27:40 -060026#include <sys/statvfs.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080027
Elliott Hughese4ec9eb2015-12-04 15:39:32 -080028#include <android-base/logging.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080029#include <android-base/stringprintf.h>
30#include <cutils/fs.h>
Jeff Sharkey871a8f22017-02-21 18:30:28 -070031#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070032#include <log/log.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080033#include <private/android_filesystem_config.h>
Jeff Sharkeyc03de092015-04-07 18:14:05 -070034
Andreas Gampe02d0de52015-11-11 20:43:16 -080035#include "globals.h" // extern variables.
36
37#ifndef LOG_TAG
38#define LOG_TAG "installd"
39#endif
Jeff Sharkey9a998f42016-07-14 18:16:22 -060040
Mike Lockwood94afecf2012-10-24 10:45:23 -070041#define CACHE_NOISY(x) //x
Jeff Sharkey9a998f42016-07-14 18:16:22 -060042#define DEBUG_XATTRS 0
Mike Lockwood94afecf2012-10-24 10:45:23 -070043
Jeff Sharkeyc03de092015-04-07 18:14:05 -070044using android::base::StringPrintf;
Mike Lockwood94afecf2012-10-24 10:45:23 -070045
Andreas Gampe02d0de52015-11-11 20:43:16 -080046namespace android {
47namespace installd {
48
Jeff Sharkeyc03de092015-04-07 18:14:05 -070049/**
50 * Check that given string is valid filename, and that it attempts no
51 * parent or child directory traversal.
52 */
Jeff Sharkey423e7462016-12-09 18:18:43 -070053bool is_valid_filename(const std::string& name) {
Jeff Sharkeyc03de092015-04-07 18:14:05 -070054 if (name.empty() || (name == ".") || (name == "..")
55 || (name.find('/') != std::string::npos)) {
56 return false;
57 } else {
58 return true;
59 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070060}
61
Calin Juravle6a1648e2016-02-01 12:12:16 +000062static void check_package_name(const char* package_name) {
63 CHECK(is_valid_filename(package_name));
Jeff Sharkey423e7462016-12-09 18:18:43 -070064 CHECK(is_valid_package_name(package_name));
Calin Juravle6a1648e2016-02-01 12:12:16 +000065}
66
Mike Lockwood94afecf2012-10-24 10:45:23 -070067/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -070068 * Create the path name where package app contents should be stored for
69 * the given volume UUID and package name. An empty UUID is assumed to
70 * be internal storage.
71 */
72std::string create_data_app_package_path(const char* volume_uuid,
73 const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +000074 check_package_name(package_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -070075 return StringPrintf("%s/%s",
76 create_data_app_path(volume_uuid).c_str(), package_name);
77}
78
79/**
Jeff Sharkeyc03de092015-04-07 18:14:05 -070080 * Create the path name where package data should be stored for the given
81 * volume UUID, package name, and user ID. An empty UUID is assumed to be
82 * internal storage.
Mike Lockwood94afecf2012-10-24 10:45:23 -070083 */
Jeff Sharkey2f720f72016-04-10 20:51:40 -060084std::string create_data_user_ce_package_path(const char* volume_uuid,
Jeff Sharkeyd7921182015-04-30 15:58:19 -070085 userid_t user, const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +000086 check_package_name(package_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -070087 return StringPrintf("%s/%s",
Jeff Sharkey2f720f72016-04-10 20:51:40 -060088 create_data_user_ce_path(volume_uuid, user).c_str(), package_name);
89}
90
91std::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user,
92 const char* package_name, ino_t ce_data_inode) {
93 // For testing purposes, rely on the inode when defined; this could be
94 // optimized to use access() in the future.
95 auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name);
96 if (ce_data_inode != 0) {
97 auto user_path = create_data_user_ce_path(volume_uuid, user);
98 DIR* dir = opendir(user_path.c_str());
99 if (dir == nullptr) {
100 PLOG(ERROR) << "Failed to opendir " << user_path;
101 return fallback;
102 }
103
104 struct dirent* ent;
105 while ((ent = readdir(dir))) {
106 if (ent->d_ino == ce_data_inode) {
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600107 auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name);
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600108#if DEBUG_XATTRS
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600109 if (resolved != fallback) {
110 LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode
111 << " instead of " << fallback;
112 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600113#endif
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600114 closedir(dir);
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600115 return resolved;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600116 }
117 }
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600118 LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600119 closedir(dir);
120 return fallback;
121 } else {
122 return fallback;
123 }
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700124}
Mike Lockwood94afecf2012-10-24 10:45:23 -0700125
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800126std::string create_data_user_de_package_path(const char* volume_uuid,
127 userid_t user, const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +0000128 check_package_name(package_name);
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800129 return StringPrintf("%s/%s",
130 create_data_user_de_path(volume_uuid, user).c_str(), package_name);
131}
132
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700133int create_pkg_path(char path[PKG_PATH_MAX], const char *pkgname,
134 const char *postfix, userid_t userid) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700135 if (!is_valid_package_name(pkgname)) {
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700136 path[0] = '\0';
Mike Lockwood94afecf2012-10-24 10:45:23 -0700137 return -1;
138 }
139
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600140 std::string _tmp(create_data_user_ce_package_path(nullptr, userid, pkgname) + postfix);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700141 const char* tmp = _tmp.c_str();
142 if (strlen(tmp) >= PKG_PATH_MAX) {
143 path[0] = '\0';
144 return -1;
145 } else {
146 strcpy(path, tmp);
147 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700149}
150
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700151std::string create_data_path(const char* volume_uuid) {
152 if (volume_uuid == nullptr) {
153 return "/data";
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700154 } else if (!strcmp(volume_uuid, "TEST")) {
155 CHECK(property_get_bool("ro.debuggable", false));
156 return "/data/local/tmp";
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700157 } else {
158 CHECK(is_valid_filename(volume_uuid));
159 return StringPrintf("/mnt/expand/%s", volume_uuid);
160 }
161}
162
Mike Lockwood94afecf2012-10-24 10:45:23 -0700163/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700164 * Create the path name for app data.
165 */
166std::string create_data_app_path(const char* volume_uuid) {
167 return StringPrintf("%s/app", create_data_path(volume_uuid).c_str());
168}
169
170/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700171 * Create the path name for user data for a certain userid.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700172 */
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600173std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) {
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700174 std::string data(create_data_path(volume_uuid));
175 if (volume_uuid == nullptr) {
176 if (userid == 0) {
177 return StringPrintf("%s/data", data.c_str());
178 } else {
179 return StringPrintf("%s/user/%u", data.c_str(), userid);
180 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181 } else {
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700182 return StringPrintf("%s/user/%u", data.c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700183 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184}
185
186/**
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800187 * Create the path name for device encrypted user data for a certain userid.
188 */
189std::string create_data_user_de_path(const char* volume_uuid, userid_t userid) {
190 std::string data(create_data_path(volume_uuid));
191 return StringPrintf("%s/user_de/%u", data.c_str(), userid);
192}
193
194/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700195 * Create the path name for media for a certain userid.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700196 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700197std::string create_data_media_path(const char* volume_uuid, userid_t userid) {
198 return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700199}
200
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700201std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) {
202 return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name);
203}
204
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700205std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
206 const char* data_type, const char* package_name) {
207 return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(),
208 data_type, package_name);
209}
210
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600211std::string create_data_misc_legacy_path(userid_t userid) {
212 return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid);
213}
214
Calin Juravle114f0812017-03-08 19:05:07 -0800215std::string create_primary_cur_profile_dir_path(userid_t userid) {
Calin Juravle6a1648e2016-02-01 12:12:16 +0000216 return StringPrintf("%s/cur/%u", android_profiles_dir.path, userid);
217}
218
Calin Juravle114f0812017-03-08 19:05:07 -0800219std::string create_primary_current_profile_package_dir_path(userid_t user,
220 const std::string& package_name) {
Calin Juravle76268c52017-03-09 13:19:42 -0800221 check_package_name(package_name.c_str());
Calin Juravle114f0812017-03-08 19:05:07 -0800222 return StringPrintf("%s/%s",
223 create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str());
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700224}
225
Calin Juravle114f0812017-03-08 19:05:07 -0800226std::string create_primary_ref_profile_dir_path() {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700227 return StringPrintf("%s/ref", android_profiles_dir.path);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000228}
229
Calin Juravle114f0812017-03-08 19:05:07 -0800230std::string create_primary_reference_profile_package_dir_path(const std::string& package_name) {
Calin Juravle76268c52017-03-09 13:19:42 -0800231 check_package_name(package_name.c_str());
232 return StringPrintf("%s/ref/%s", android_profiles_dir.path, package_name.c_str());
Calin Juravle6a1648e2016-02-01 12:12:16 +0000233}
234
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700235std::string create_data_dalvik_cache_path() {
236 return "/data/dalvik-cache";
237}
238
Calin Juravle114f0812017-03-08 19:05:07 -0800239// Keep profile paths in sync with ActivityThread and LoadedApk.
240const std::string PROFILE_EXT = ".prof";
241const std::string PRIMARY_PROFILE_NAME = "primary" + PROFILE_EXT;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700242
Calin Juravle114f0812017-03-08 19:05:07 -0800243std::string create_current_profile_path(userid_t user, const std::string& location,
244 bool is_secondary_dex) {
245 if (is_secondary_dex) {
246 // Secondary dex profiles are stored next to the dex files using .prof extension.
247 return StringPrintf("%s%s", location.c_str(), PROFILE_EXT.c_str());
248 } else {
249 // Profiles for primary apks are under /data/misc/profiles/cur.
250 std::string profile_dir = create_primary_current_profile_package_dir_path(user, location);
251 return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
252 }
253}
254
255std::string create_reference_profile_path(const std::string& location, bool is_secondary_dex) {
256 if (is_secondary_dex) {
257 // Secondary dex reference profiles are stored next to the dex files under the oat folder.
258 size_t dirIndex = location.rfind('/');
259 CHECK(dirIndex != std::string::npos)
260 << "Unexpected dir structure for secondary dex " << location;
261
262 std::string dex_dir = location.substr(0, dirIndex);
263 std::string dex_name = location.substr(dirIndex +1);
264 return StringPrintf("%s/oat/%s%s",
265 dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str());
266 } else {
267 // Reference profiles for primary apks are stored in /data/misc/profile/ref.
268 std::string profile_dir = create_primary_reference_profile_package_dir_path(location);
269 return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
270 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700271}
272
Jeff Sharkeye3637242015-04-08 20:56:42 -0700273std::vector<userid_t> get_known_users(const char* volume_uuid) {
274 std::vector<userid_t> users;
275
276 // We always have an owner
277 users.push_back(0);
278
279 std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX);
280 DIR* dir = opendir(path.c_str());
281 if (dir == NULL) {
282 // Unable to discover other users, but at least return owner
283 PLOG(ERROR) << "Failed to opendir " << path;
284 return users;
285 }
286
287 struct dirent* ent;
288 while ((ent = readdir(dir))) {
289 if (ent->d_type != DT_DIR) {
290 continue;
291 }
292
293 char* end;
294 userid_t user = strtol(ent->d_name, &end, 10);
295 if (*end == '\0' && user != 0) {
296 LOG(DEBUG) << "Found valid user " << user;
297 users.push_back(user);
298 }
299 }
300 closedir(dir);
301
302 return users;
303}
304
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700305int calculate_tree_size(const std::string& path, int64_t* size,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700306 int32_t include_gid, int32_t exclude_gid, bool exclude_apps) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700307 FTS *fts;
308 FTSENT *p;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700309 int64_t matchedSize = 0;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700310 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700311 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700312 if (errno != ENOENT) {
313 PLOG(ERROR) << "Failed to fts_open " << path;
314 }
315 return -1;
316 }
317 while ((p = fts_read(fts)) != NULL) {
318 switch (p->fts_info) {
319 case FTS_D:
320 case FTS_DEFAULT:
321 case FTS_F:
322 case FTS_SL:
323 case FTS_SLNONE:
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700324 int32_t uid = p->fts_statp->st_uid;
325 int32_t gid = p->fts_statp->st_gid;
326 int32_t user_uid = multiuser_get_app_id(uid);
327 int32_t user_gid = multiuser_get_app_id(gid);
328 if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END)
329 || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END)
330 || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) {
331 // Don't traverse inside or measure
332 fts_set(fts, p, FTS_SKIP);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700333 break;
334 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700335 if (include_gid != -1 && gid != include_gid) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700336 break;
337 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700338 if (exclude_gid != -1 && gid == exclude_gid) {
339 break;
340 }
341 matchedSize += (p->fts_statp->st_blocks * 512);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700342 break;
343 }
344 }
345 fts_close(fts);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700346#if MEASURE_DEBUG
347 if ((include_gid == -1) && (exclude_gid == -1)) {
348 LOG(DEBUG) << "Measured " << path << " size " << matchedSize;
349 } else {
350 LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid
351 << " exclude " << exclude_gid;
352 }
353#endif
354 *size += matchedSize;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700355 return 0;
356}
357
Mike Lockwood94afecf2012-10-24 10:45:23 -0700358int create_move_path(char path[PKG_PATH_MAX],
359 const char* pkgname,
360 const char* leaf,
Andreas Gampe02d0de52015-11-11 20:43:16 -0800361 userid_t userid ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700362{
363 if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1)
364 >= PKG_PATH_MAX) {
365 return -1;
366 }
367
368 sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf);
369 return 0;
370}
371
372/**
373 * Checks whether the package name is valid. Returns -1 on error and
374 * 0 on success.
375 */
Jeff Sharkey423e7462016-12-09 18:18:43 -0700376bool is_valid_package_name(const std::string& packageName) {
Jeff Sharkey367ace22017-03-07 22:12:03 -0700377 // This logic is borrowed from PackageParser.java
378 bool hasSep = false;
379 bool front = true;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700380
Jeff Sharkey367ace22017-03-07 22:12:03 -0700381 auto it = packageName.begin();
382 for (; it != packageName.end() && *it != '-'; it++) {
383 char c = *it;
384 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
385 front = false;
386 continue;
387 }
388 if (!front) {
389 if ((c >= '0' && c <= '9') || c == '_') {
390 continue;
391 }
392 }
393 if (c == '.') {
394 hasSep = true;
395 front = true;
396 continue;
397 }
398 LOG(WARNING) << "Bad package character " << c << " in " << packageName;
Jeff Sharkey423e7462016-12-09 18:18:43 -0700399 return false;
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700400 }
401
Jeff Sharkeyab7ac8d2017-03-08 12:39:46 -0700402 if (front) {
Jeff Sharkey367ace22017-03-07 22:12:03 -0700403 LOG(WARNING) << "Missing separator in " << packageName;
404 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700405 }
406
Jeff Sharkey367ace22017-03-07 22:12:03 -0700407 for (; it != packageName.end(); it++) {
408 char c = *it;
409 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue;
410 if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue;
411 LOG(WARNING) << "Bad suffix character " << c << " in " << packageName;
412 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700413 }
414
Jeff Sharkey423e7462016-12-09 18:18:43 -0700415 return true;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700416}
417
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100418static int _delete_dir_contents(DIR *d,
419 int (*exclusion_predicate)(const char *name, const int is_dir))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700420{
421 int result = 0;
422 struct dirent *de;
423 int dfd;
424
425 dfd = dirfd(d);
426
427 if (dfd < 0) return -1;
428
429 while ((de = readdir(d))) {
430 const char *name = de->d_name;
431
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100432 /* check using the exclusion predicate, if provided */
433 if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
434 continue;
435 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700436
437 if (de->d_type == DT_DIR) {
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700438 int subfd;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700439 DIR *subdir;
440
441 /* always skip "." and ".." */
442 if (name[0] == '.') {
443 if (name[1] == 0) continue;
444 if ((name[1] == '.') && (name[2] == 0)) continue;
445 }
446
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700447 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700448 if (subfd < 0) {
449 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
450 result = -1;
451 continue;
452 }
453 subdir = fdopendir(subfd);
454 if (subdir == NULL) {
455 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
456 close(subfd);
457 result = -1;
458 continue;
459 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100460 if (_delete_dir_contents(subdir, exclusion_predicate)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700461 result = -1;
462 }
463 closedir(subdir);
464 if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
465 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
466 result = -1;
467 }
468 } else {
469 if (unlinkat(dfd, name, 0) < 0) {
470 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
471 result = -1;
472 }
473 }
474 }
475
476 return result;
477}
478
Calin Juravleb06f98a2016-03-28 15:11:01 +0100479int delete_dir_contents(const std::string& pathname, bool ignore_if_missing) {
480 return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700481}
482
Calin Juravleb06f98a2016-03-28 15:11:01 +0100483int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) {
484 return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700485}
486
Mike Lockwood94afecf2012-10-24 10:45:23 -0700487int delete_dir_contents(const char *pathname,
488 int also_delete_dir,
Calin Juravleb06f98a2016-03-28 15:11:01 +0100489 int (*exclusion_predicate)(const char*, const int),
490 bool ignore_if_missing)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700491{
492 int res = 0;
493 DIR *d;
494
495 d = opendir(pathname);
496 if (d == NULL) {
Calin Juravleb06f98a2016-03-28 15:11:01 +0100497 if (ignore_if_missing && (errno == ENOENT)) {
498 return 0;
499 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700500 ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
501 return -errno;
502 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100503 res = _delete_dir_contents(d, exclusion_predicate);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700504 closedir(d);
505 if (also_delete_dir) {
506 if (rmdir(pathname)) {
507 ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
508 res = -1;
509 }
510 }
511 return res;
512}
513
514int delete_dir_contents_fd(int dfd, const char *name)
515{
516 int fd, res;
517 DIR *d;
518
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700519 fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700520 if (fd < 0) {
521 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
522 return -1;
523 }
524 d = fdopendir(fd);
525 if (d == NULL) {
526 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
527 close(fd);
528 return -1;
529 }
530 res = _delete_dir_contents(d, 0);
531 closedir(d);
532 return res;
533}
534
Robin Lee60fd3fe2014-10-07 16:55:02 +0100535static int _copy_owner_permissions(int srcfd, int dstfd)
536{
537 struct stat st;
538 if (fstat(srcfd, &st) != 0) {
539 return -1;
540 }
541 if (fchmod(dstfd, st.st_mode) != 0) {
542 return -1;
543 }
544 return 0;
545}
546
547static int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
548{
549 int result = 0;
550 if (_copy_owner_permissions(sdfd, ddfd) != 0) {
551 ALOGE("_copy_dir_files failed to copy dir permissions\n");
552 }
553 if (fchown(ddfd, owner, group) != 0) {
554 ALOGE("_copy_dir_files failed to change dir owner\n");
555 }
556
557 DIR *ds = fdopendir(sdfd);
558 if (ds == NULL) {
559 ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
560 return -1;
561 }
562 struct dirent *de;
563 while ((de = readdir(ds))) {
564 if (de->d_type != DT_REG) {
565 continue;
566 }
567
568 const char *name = de->d_name;
569 int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
570 int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
571 if (fsfd == -1 || fdfd == -1) {
572 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
573 } else {
574 if (_copy_owner_permissions(fsfd, fdfd) != 0) {
575 ALOGE("Failed to change file permissions\n");
576 }
577 if (fchown(fdfd, owner, group) != 0) {
578 ALOGE("Failed to change file owner\n");
579 }
580
581 char buf[8192];
582 ssize_t size;
583 while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
584 write(fdfd, buf, size);
585 }
586 if (size < 0) {
587 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
588 result = -1;
589 }
590 }
591 close(fdfd);
592 close(fsfd);
593 }
594
595 return result;
596}
597
598int copy_dir_files(const char *srcname,
599 const char *dstname,
600 uid_t owner,
601 uid_t group)
602{
603 int res = 0;
604 DIR *ds = NULL;
605 DIR *dd = NULL;
606
607 ds = opendir(srcname);
608 if (ds == NULL) {
609 ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
610 return -errno;
611 }
612
613 mkdir(dstname, 0600);
614 dd = opendir(dstname);
615 if (dd == NULL) {
616 ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
617 closedir(ds);
618 return -errno;
619 }
620
621 int sdfd = dirfd(ds);
622 int ddfd = dirfd(dd);
623 if (sdfd != -1 && ddfd != -1) {
624 res = _copy_dir_files(sdfd, ddfd, owner, group);
625 } else {
626 res = -errno;
627 }
628 closedir(dd);
629 closedir(ds);
630 return res;
631}
632
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700633int64_t data_disk_free(const std::string& data_path)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700634{
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600635 struct statvfs sfs;
636 if (statvfs(data_path.c_str(), &sfs) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700637 return sfs.f_bavail * sfs.f_bsize;
638 } else {
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600639 PLOG(ERROR) << "Couldn't statvfs " << data_path;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700640 return -1;
641 }
642}
643
644cache_t* start_cache_collection()
645{
646 cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t));
647 return cache;
648}
649
650#define CACHE_BLOCK_SIZE (512*1024)
651
652static void* _cache_malloc(cache_t* cache, size_t len)
653{
654 len = (len+3)&~3;
655 if (len > (CACHE_BLOCK_SIZE/2)) {
656 // It doesn't make sense to try to put this allocation into one
657 // of our blocks, because it is so big. Instead, make a new dedicated
658 // block for it.
659 int8_t* res = (int8_t*)malloc(len+sizeof(void*));
660 if (res == NULL) {
661 return NULL;
662 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600663 CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %zu", res, len));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700664 // Link it into our list of blocks, not disrupting the current one.
665 if (cache->memBlocks == NULL) {
666 *(void**)res = NULL;
667 cache->memBlocks = res;
668 } else {
669 *(void**)res = *(void**)cache->memBlocks;
670 *(void**)cache->memBlocks = res;
671 }
672 return res + sizeof(void*);
673 }
674 int8_t* res = cache->curMemBlockAvail;
675 int8_t* nextPos = res + len;
676 if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) {
Jeff Sharkey19803802015-04-07 12:44:51 -0700677 int8_t* newBlock = (int8_t*) malloc(CACHE_BLOCK_SIZE);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700678 if (newBlock == NULL) {
679 return NULL;
680 }
681 CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock));
682 *(void**)newBlock = cache->memBlocks;
683 cache->memBlocks = newBlock;
684 res = cache->curMemBlockAvail = newBlock + sizeof(void*);
685 cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE;
686 nextPos = res + len;
687 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600688 CACHE_NOISY(ALOGI("cache_malloc: ret %p size %zu, block=%p, nextPos=%p",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700689 res, len, cache->memBlocks, nextPos));
690 cache->curMemBlockAvail = nextPos;
691 return res;
692}
693
694static void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len)
695{
696 // This isn't really a realloc, but it is good enough for our purposes here.
697 void* alloc = _cache_malloc(cache, len);
698 if (alloc != NULL && cur != NULL) {
699 memcpy(alloc, cur, origLen < len ? origLen : len);
700 }
701 return alloc;
702}
703
704static void _inc_num_cache_collected(cache_t* cache)
705{
706 cache->numCollected++;
707 if ((cache->numCollected%20000) == 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700708 ALOGI("Collected cache so far: %zd directories, %zd files",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700709 cache->numDirs, cache->numFiles);
710 }
711}
712
713static cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name)
714{
715 size_t nameLen = strlen(name);
716 cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1);
717 if (dir != NULL) {
718 dir->parent = parent;
719 dir->childCount = 0;
720 dir->hiddenCount = 0;
721 dir->deleted = 0;
722 strcpy(dir->name, name);
723 if (cache->numDirs >= cache->availDirs) {
724 size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2;
725 cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs,
726 cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*));
727 if (newDirs == NULL) {
728 ALOGE("Failure growing cache dirs array for %s\n", name);
729 return NULL;
730 }
731 cache->availDirs = newAvail;
732 cache->dirs = newDirs;
733 }
734 cache->dirs[cache->numDirs] = dir;
735 cache->numDirs++;
736 if (parent != NULL) {
737 parent->childCount++;
738 }
739 _inc_num_cache_collected(cache);
740 } else {
741 ALOGE("Failure allocating cache_dir_t for %s\n", name);
742 }
743 return dir;
744}
745
746static cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime,
747 const char *name)
748{
749 size_t nameLen = strlen(name);
750 cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1);
751 if (file != NULL) {
752 file->dir = dir;
753 file->modTime = modTime;
754 strcpy(file->name, name);
755 if (cache->numFiles >= cache->availFiles) {
756 size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2;
757 cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files,
758 cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*));
759 if (newFiles == NULL) {
760 ALOGE("Failure growing cache file array for %s\n", name);
761 return NULL;
762 }
763 cache->availFiles = newAvail;
764 cache->files = newFiles;
765 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600766 CACHE_NOISY(ALOGI("Setting file %p at position %zd in array %p", file,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700767 cache->numFiles, cache->files));
768 cache->files[cache->numFiles] = file;
769 cache->numFiles++;
770 dir->childCount++;
771 _inc_num_cache_collected(cache);
772 } else {
773 ALOGE("Failure allocating cache_file_t for %s\n", name);
774 }
775 return file;
776}
777
778static int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName,
779 DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen)
780{
781 struct dirent *de;
782 cache_dir_t* cacheDir = NULL;
783 int dfd;
784
785 CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s",
786 parentDir, dirName, dir, pathBase));
787
788 dfd = dirfd(dir);
789
790 if (dfd < 0) return 0;
791
792 // Sub-directories always get added to the data structure, so if they
793 // are empty we will know about them to delete them later.
794 cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
795
796 while ((de = readdir(dir))) {
797 const char *name = de->d_name;
798
799 if (de->d_type == DT_DIR) {
800 int subfd;
801 DIR *subdir;
802
803 /* always skip "." and ".." */
804 if (name[0] == '.') {
805 if (name[1] == 0) continue;
806 if ((name[1] == '.') && (name[2] == 0)) continue;
807 }
808
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700809 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700810 if (subfd < 0) {
811 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
812 continue;
813 }
814 subdir = fdopendir(subfd);
815 if (subdir == NULL) {
816 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
817 close(subfd);
818 continue;
819 }
820 if (cacheDir == NULL) {
821 cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
822 }
823 if (cacheDir != NULL) {
824 // Update pathBase for the new path... this may change dirName
825 // if that is also pointing to the path, but we are done with it
826 // now.
827 size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
828 CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase));
829 if (finallen < pathAvailLen) {
830 _add_cache_files(cache, cacheDir, name, subdir, pathBase,
831 pathPos+finallen, pathAvailLen-finallen);
832 } else {
833 // Whoops, the final path is too long! We'll just delete
834 // this directory.
835 ALOGW("Cache dir %s truncated in path %s; deleting dir\n",
836 name, pathBase);
837 _delete_dir_contents(subdir, NULL);
838 if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
839 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
840 }
841 }
842 }
843 closedir(subdir);
844 } else if (de->d_type == DT_REG) {
845 // Skip files that start with '.'; they will be deleted if
846 // their entire directory is deleted. This allows for metadata
847 // like ".nomedia" to remain in the directory until the entire
848 // directory is deleted.
849 if (cacheDir == NULL) {
850 cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
851 }
852 if (name[0] == '.') {
853 cacheDir->hiddenCount++;
854 continue;
855 }
856 if (cacheDir != NULL) {
857 // Build final full path for file... this may change dirName
858 // if that is also pointing to the path, but we are done with it
859 // now.
860 size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
861 CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase));
862 if (finallen < pathAvailLen) {
863 struct stat s;
864 if (stat(pathBase, &s) >= 0) {
865 _add_cache_file_t(cache, cacheDir, s.st_mtime, name);
866 } else {
867 ALOGW("Unable to stat cache file %s; deleting\n", pathBase);
868 if (unlink(pathBase) < 0) {
869 ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno));
870 }
871 }
872 } else {
873 // Whoops, the final path is too long! We'll just delete
874 // this file.
875 ALOGW("Cache file %s truncated in path %s; deleting\n",
876 name, pathBase);
877 if (unlinkat(dfd, name, 0) < 0) {
878 *pathPos = 0;
879 ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase,
880 strerror(errno));
881 }
882 }
883 }
884 } else {
885 cacheDir->hiddenCount++;
886 }
887 }
888 return 0;
889}
890
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600891int get_path_inode(const std::string& path, ino_t *inode) {
892 struct stat buf;
893 memset(&buf, 0, sizeof(buf));
894 if (stat(path.c_str(), &buf) != 0) {
895 PLOG(WARNING) << "Failed to stat " << path;
896 return -1;
897 } else {
898 *inode = buf.st_ino;
899 return 0;
900 }
901}
902
903/**
904 * Write the inode of a specific child file into the given xattr on the
905 * parent directory. This allows you to find the child later, even if its
906 * name is encrypted.
907 */
908int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
909 ino_t inode = 0;
910 uint64_t inode_raw = 0;
911 auto path = StringPrintf("%s/%s", parent.c_str(), name);
912
913 if (get_path_inode(path, &inode) != 0) {
914 // Path probably doesn't exist yet; ignore
915 return 0;
916 }
917
918 // Check to see if already set correctly
919 if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
920 if (inode_raw == inode) {
921 // Already set correctly; skip writing
922 return 0;
923 } else {
924 PLOG(WARNING) << "Mismatched inode value; found " << inode
925 << " on disk but marked value was " << inode_raw << "; overwriting";
926 }
927 }
928
929 inode_raw = inode;
Jeff Sharkey4ed65072016-07-22 11:38:54 -0600930 if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) {
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600931 PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent;
932 return -1;
933 } else {
934 return 0;
935 }
936}
937
938/**
939 * Read the inode of a specific child file from the given xattr on the
940 * parent directory. Returns a currently valid path for that child, which
941 * might have an encrypted name.
942 */
943std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
944 ino_t inode = 0;
945 uint64_t inode_raw = 0;
946 auto fallback = StringPrintf("%s/%s", parent.c_str(), name);
947
948 // Lookup the inode value written earlier
949 if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
950 inode = inode_raw;
951 }
952
953 // For testing purposes, rely on the inode when defined; this could be
954 // optimized to use access() in the future.
955 if (inode != 0) {
956 DIR* dir = opendir(parent.c_str());
957 if (dir == nullptr) {
958 PLOG(ERROR) << "Failed to opendir " << parent;
959 return fallback;
960 }
961
962 struct dirent* ent;
963 while ((ent = readdir(dir))) {
964 if (ent->d_ino == inode) {
965 auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name);
966#if DEBUG_XATTRS
967 if (resolved != fallback) {
968 LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode
969 << " instead of " << fallback;
970 }
971#endif
972 closedir(dir);
973 return resolved;
974 }
975 }
976 LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback;
977 closedir(dir);
978 return fallback;
979 } else {
980 return fallback;
981 }
982}
983
Jeff Sharkey54e292e2016-05-10 17:21:13 -0600984void add_cache_files(cache_t* cache, const std::string& data_path) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700985 DIR *d;
986 struct dirent *de;
987 char dirname[PATH_MAX];
988
Jeff Sharkey54e292e2016-05-10 17:21:13 -0600989 const char* basepath = data_path.c_str();
990 CACHE_NOISY(ALOGI("add_cache_files: basepath=%s\n", basepath));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700991
992 d = opendir(basepath);
993 if (d == NULL) {
994 return;
995 }
996
997 while ((de = readdir(d))) {
998 if (de->d_type == DT_DIR) {
999 DIR* subdir;
1000 const char *name = de->d_name;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001001
1002 /* always skip "." and ".." */
1003 if (name[0] == '.') {
1004 if (name[1] == 0) continue;
1005 if ((name[1] == '.') && (name[2] == 0)) continue;
1006 }
1007
Jeff Sharkey9a998f42016-07-14 18:16:22 -06001008 auto parent = StringPrintf("%s/%s", basepath, name);
1009 auto resolved = read_path_inode(parent, "cache", kXattrInodeCache);
1010 strcpy(dirname, resolved.c_str());
Mike Lockwood94afecf2012-10-24 10:45:23 -07001011 CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname));
Jeff Sharkey54e292e2016-05-10 17:21:13 -06001012
Mike Lockwood94afecf2012-10-24 10:45:23 -07001013 subdir = opendir(dirname);
1014 if (subdir != NULL) {
1015 size_t dirnameLen = strlen(dirname);
1016 _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen,
1017 PATH_MAX - dirnameLen);
1018 closedir(subdir);
1019 }
1020 }
1021 }
1022
1023 closedir(d);
1024}
1025
Fyodor Kupolovf3124d82016-09-07 13:07:45 -07001026void add_preloads_file_cache(cache_t* cache, const char* volume_uuid) {
1027 char dirname[PATH_MAX];
1028 DIR* subdir;
1029 auto cache_path = StringPrintf("%s/preloads/file_cache", create_data_path(volume_uuid).c_str());
1030 strcpy(dirname, cache_path.c_str());
1031 CACHE_NOISY(ALOGI("add_preloads_file_cache: dirname=%s\n", dirname));
1032 subdir = opendir(dirname);
1033 if (subdir != NULL) {
1034 size_t dirnameLen = strlen(dirname);
1035 _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname + dirnameLen,
1036 PATH_MAX - dirnameLen);
1037 closedir(subdir);
1038 }
1039}
1040
Mike Lockwood94afecf2012-10-24 10:45:23 -07001041static char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir)
1042{
1043 char *pos = path;
1044 if (dir->parent != NULL) {
1045 pos = create_dir_path(path, dir->parent);
1046 }
1047 // Note that we don't need to worry about going beyond the buffer,
1048 // since when we were constructing the cache entries our maximum
1049 // buffer size for full paths was PATH_MAX.
1050 strcpy(pos, dir->name);
1051 pos += strlen(pos);
1052 *pos = '/';
1053 pos++;
1054 *pos = 0;
1055 return pos;
1056}
1057
1058static void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir)
1059{
1060 if (dir->parent != NULL) {
1061 create_dir_path(path, dir);
1062 ALOGI("DEL DIR %s\n", path);
1063 if (dir->hiddenCount <= 0) {
1064 if (rmdir(path)) {
1065 ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno));
1066 return;
1067 }
1068 } else {
1069 // The directory contains hidden files so we need to delete
1070 // them along with the directory itself.
1071 if (delete_dir_contents(path, 1, NULL)) {
1072 return;
1073 }
1074 }
1075 dir->parent->childCount--;
1076 dir->deleted = 1;
1077 if (dir->parent->childCount <= 0) {
1078 delete_cache_dir(path, dir->parent);
1079 }
1080 } else if (dir->hiddenCount > 0) {
1081 // This is a root directory, but it has hidden files. Get rid of
1082 // all of those files, but not the directory itself.
1083 create_dir_path(path, dir);
1084 ALOGI("DEL CONTENTS %s\n", path);
1085 delete_dir_contents(path, 0, NULL);
1086 }
1087}
1088
1089static int cache_modtime_sort(const void *lhsP, const void *rhsP)
1090{
1091 const cache_file_t *lhs = *(const cache_file_t**)lhsP;
1092 const cache_file_t *rhs = *(const cache_file_t**)rhsP;
1093 return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0);
1094}
1095
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001096void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001097{
1098 size_t i;
1099 int skip = 0;
1100 char path[PATH_MAX];
1101
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001102 ALOGI("Collected cache files: %zd directories, %zd files",
Mike Lockwood94afecf2012-10-24 10:45:23 -07001103 cache->numDirs, cache->numFiles);
1104
1105 CACHE_NOISY(ALOGI("Sorting files..."));
1106 qsort(cache->files, cache->numFiles, sizeof(cache_file_t*),
1107 cache_modtime_sort);
1108
1109 CACHE_NOISY(ALOGI("Cleaning empty directories..."));
1110 for (i=cache->numDirs; i>0; i--) {
1111 cache_dir_t* dir = cache->dirs[i-1];
1112 if (dir->childCount <= 0 && !dir->deleted) {
1113 delete_cache_dir(path, dir);
1114 }
1115 }
1116
1117 CACHE_NOISY(ALOGI("Trimming files..."));
1118 for (i=0; i<cache->numFiles; i++) {
1119 skip++;
1120 if (skip > 10) {
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001121 if (data_disk_free(data_path) > free_size) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001122 return;
1123 }
1124 skip = 0;
1125 }
1126 cache_file_t* file = cache->files[i];
1127 strcpy(create_dir_path(path, file->dir), file->name);
1128 ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path);
1129 if (unlink(path) < 0) {
1130 ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno));
1131 }
1132 file->dir->childCount--;
1133 if (file->dir->childCount <= 0) {
1134 delete_cache_dir(path, file->dir);
1135 }
1136 }
1137}
1138
1139void finish_cache_collection(cache_t* cache)
1140{
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -07001141 CACHE_NOISY(size_t i;)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001142
Jeff Sharkey9a998f42016-07-14 18:16:22 -06001143 CACHE_NOISY(ALOGI("clear_cache_files: %zu dirs, %zu files\n", cache->numDirs, cache->numFiles));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001144 CACHE_NOISY(
1145 for (i=0; i<cache->numDirs; i++) {
1146 cache_dir_t* dir = cache->dirs[i];
Jeff Sharkey9a998f42016-07-14 18:16:22 -06001147 ALOGI("dir #%zu: %p %s parent=%p\n", i, dir, dir->name, dir->parent);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001148 })
1149 CACHE_NOISY(
1150 for (i=0; i<cache->numFiles; i++) {
1151 cache_file_t* file = cache->files[i];
Jeff Sharkey9a998f42016-07-14 18:16:22 -06001152 ALOGI("file #%zu: %p %s time=%d dir=%p\n", i, file, file->name,
Mike Lockwood94afecf2012-10-24 10:45:23 -07001153 (int)file->modTime, file->dir);
1154 })
1155 void* block = cache->memBlocks;
1156 while (block != NULL) {
1157 void* nextBlock = *(void**)block;
1158 CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block));
1159 free(block);
1160 block = nextBlock;
1161 }
1162 free(cache);
1163}
1164
1165/**
Calin Juravlec597b6d2014-08-19 17:43:05 +01001166 * Validate that the path is valid in the context of the provided directory.
1167 * The path is allowed to have at most one subdirectory and no indirections
1168 * to top level directories (i.e. have "..").
1169 */
Jeff Sharkeye23a1322015-04-06 16:19:39 -07001170static int validate_path(const dir_rec_t* dir, const char* path, int maxSubdirs) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001171 size_t dir_len = dir->len;
1172 const char* subdir = strchr(path + dir_len, '/');
1173
1174 // Only allow the path to have at most one subdirectory.
1175 if (subdir != NULL) {
1176 ++subdir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -07001177 if ((--maxSubdirs == 0) && strchr(subdir, '/') != NULL) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001178 ALOGE("invalid apk path '%s' (subdir?)\n", path);
1179 return -1;
1180 }
1181 }
1182
1183 // Directories can't have a period directly after the directory markers to prevent "..".
1184 if ((path[dir_len] == '.') || ((subdir != NULL) && (*subdir == '.'))) {
1185 ALOGE("invalid apk path '%s' (trickery)\n", path);
1186 return -1;
1187 }
1188
1189 return 0;
1190}
1191
1192/**
Mike Lockwood94afecf2012-10-24 10:45:23 -07001193 * Checks whether a path points to a system app (.apk file). Returns 0
1194 * if it is a system app or -1 if it is not.
1195 */
1196int validate_system_app_path(const char* path) {
1197 size_t i;
1198
1199 for (i = 0; i < android_system_dirs.count; i++) {
1200 const size_t dir_len = android_system_dirs.dirs[i].len;
1201 if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) {
Jeff Sharkeye23a1322015-04-06 16:19:39 -07001202 return validate_path(android_system_dirs.dirs + i, path, 1);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001203 }
1204 }
1205
1206 return -1;
1207}
1208
Calin Juravle114f0812017-03-08 19:05:07 -08001209bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
Calin Juravle42451c02017-01-17 14:43:25 -08001210 const char* volume_uuid, int uid, int storage_flag) {
1211 CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE);
1212
1213 std::string app_private_dir = storage_flag == FLAG_STORAGE_CE
Calin Juravle114f0812017-03-08 19:05:07 -08001214 ? create_data_user_ce_package_path(
1215 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str())
1216 : create_data_user_de_package_path(
1217 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str());
Calin Juravle42451c02017-01-17 14:43:25 -08001218 dir_rec_t dir;
1219 if (get_path_from_string(&dir, app_private_dir.c_str()) != 0) {
1220 LOG(WARNING) << "Could not get dir rec for " << app_private_dir;
1221 return false;
1222 }
1223 // Usually secondary dex files have a nested directory structure.
1224 // Pick at most 10 subdirectories when validating (arbitrary value).
1225 // If the secondary dex file is >10 directory nested then validation will
1226 // fail and the file will not be compiled.
Calin Juravle114f0812017-03-08 19:05:07 -08001227 return validate_path(&dir, dex_path.c_str(), /*max_subdirs*/ 10) == 0;
Calin Juravle42451c02017-01-17 14:43:25 -08001228}
1229
Mike Lockwood94afecf2012-10-24 10:45:23 -07001230/**
1231 * Get the contents of a environment variable that contains a path. Caller
1232 * owns the string that is inserted into the directory record. Returns
1233 * 0 on success and -1 on error.
1234 */
1235int get_path_from_env(dir_rec_t* rec, const char* var) {
1236 const char* path = getenv(var);
1237 int ret = get_path_from_string(rec, path);
1238 if (ret < 0) {
1239 ALOGW("Problem finding value for environment variable %s\n", var);
1240 }
1241 return ret;
1242}
1243
1244/**
1245 * Puts the string into the record as a directory. Appends '/' to the end
1246 * of all paths. Caller owns the string that is inserted into the directory
1247 * record. A null value will result in an error.
1248 *
1249 * Returns 0 on success and -1 on error.
1250 */
1251int get_path_from_string(dir_rec_t* rec, const char* path) {
1252 if (path == NULL) {
1253 return -1;
1254 } else {
1255 const size_t path_len = strlen(path);
1256 if (path_len <= 0) {
1257 return -1;
1258 }
1259
1260 // Make sure path is absolute.
1261 if (path[0] != '/') {
1262 return -1;
1263 }
1264
1265 if (path[path_len - 1] == '/') {
1266 // Path ends with a forward slash. Make our own copy.
1267
1268 rec->path = strdup(path);
1269 if (rec->path == NULL) {
1270 return -1;
1271 }
1272
1273 rec->len = path_len;
1274 } else {
1275 // Path does not end with a slash. Generate a new string.
1276 char *dst;
1277
1278 // Add space for slash and terminating null.
1279 size_t dst_size = path_len + 2;
1280
Jeff Sharkey19803802015-04-07 12:44:51 -07001281 rec->path = (char*) malloc(dst_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001282 if (rec->path == NULL) {
1283 return -1;
1284 }
1285
1286 dst = rec->path;
1287
1288 if (append_and_increment(&dst, path, &dst_size) < 0
1289 || append_and_increment(&dst, "/", &dst_size)) {
1290 ALOGE("Error canonicalizing path");
1291 return -1;
1292 }
1293
1294 rec->len = dst - rec->path;
1295 }
1296 }
1297 return 0;
1298}
1299
1300int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) {
1301 dst->len = src->len + strlen(suffix);
1302 const size_t dstSize = dst->len + 1;
1303 dst->path = (char*) malloc(dstSize);
1304
1305 if (dst->path == NULL
1306 || snprintf(dst->path, dstSize, "%s%s", src->path, suffix)
1307 != (ssize_t) dst->len) {
1308 ALOGE("Could not allocate memory to hold appended path; aborting\n");
1309 return -1;
1310 }
1311
1312 return 0;
1313}
1314
1315/**
Narayan Kamathd845c962015-06-04 13:20:27 +01001316 * Check whether path points to a valid path for an APK file. The path must
1317 * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within
1318 * that path. Returns -1 when an invalid path is encountered and 0 when a valid path
1319 * is encountered.
Mike Lockwood94afecf2012-10-24 10:45:23 -07001320 */
Narayan Kamathd845c962015-06-04 13:20:27 +01001321static int validate_apk_path_internal(const char *path, int maxSubdirs) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001322 const dir_rec_t* dir = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001323 if (!strncmp(path, android_app_dir.path, android_app_dir.len)) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001324 dir = &android_app_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001325 } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001326 dir = &android_app_private_dir;
Todd Kennedy5c1a9102015-11-23 15:18:10 -08001327 } else if (!strncmp(path, android_app_ephemeral_dir.path, android_app_ephemeral_dir.len)) {
1328 dir = &android_app_ephemeral_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001329 } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001330 dir = &android_asec_dir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -07001331 } else if (!strncmp(path, android_mnt_expand_dir.path, android_mnt_expand_dir.len)) {
1332 dir = &android_mnt_expand_dir;
Narayan Kamathd845c962015-06-04 13:20:27 +01001333 if (maxSubdirs < 2) {
1334 maxSubdirs = 2;
1335 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001336 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001337 return -1;
1338 }
1339
Jeff Sharkeye23a1322015-04-06 16:19:39 -07001340 return validate_path(dir, path, maxSubdirs);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001341}
1342
Narayan Kamathd845c962015-06-04 13:20:27 +01001343int validate_apk_path(const char* path) {
1344 return validate_apk_path_internal(path, 1 /* maxSubdirs */);
1345}
1346
1347int validate_apk_path_subdirs(const char* path) {
1348 return validate_apk_path_internal(path, 3 /* maxSubdirs */);
1349}
1350
Mike Lockwood94afecf2012-10-24 10:45:23 -07001351int append_and_increment(char** dst, const char* src, size_t* dst_size) {
1352 ssize_t ret = strlcpy(*dst, src, *dst_size);
1353 if (ret < 0 || (size_t) ret >= *dst_size) {
1354 return -1;
1355 }
1356 *dst += ret;
1357 *dst_size -= ret;
1358 return 0;
1359}
1360
Jeff Sharkey19803802015-04-07 12:44:51 -07001361char *build_string2(const char *s1, const char *s2) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001362 if (s1 == NULL || s2 == NULL) return NULL;
1363
1364 int len_s1 = strlen(s1);
1365 int len_s2 = strlen(s2);
1366 int len = len_s1 + len_s2 + 1;
Jeff Sharkey19803802015-04-07 12:44:51 -07001367 char *result = (char *) malloc(len);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001368 if (result == NULL) return NULL;
1369
1370 strcpy(result, s1);
1371 strcpy(result + len_s1, s2);
1372
1373 return result;
1374}
1375
Jeff Sharkey19803802015-04-07 12:44:51 -07001376char *build_string3(const char *s1, const char *s2, const char *s3) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001377 if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL;
1378
1379 int len_s1 = strlen(s1);
1380 int len_s2 = strlen(s2);
1381 int len_s3 = strlen(s3);
1382 int len = len_s1 + len_s2 + len_s3 + 1;
Jeff Sharkey19803802015-04-07 12:44:51 -07001383 char *result = (char *) malloc(len);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001384 if (result == NULL) return NULL;
1385
1386 strcpy(result, s1);
1387 strcpy(result + len_s1, s2);
1388 strcpy(result + len_s1 + len_s2, s3);
1389
1390 return result;
1391}
1392
Robin Lee095c7632014-04-25 15:05:19 +01001393int ensure_config_user_dirs(userid_t userid) {
Robin Lee095c7632014-04-25 15:05:19 +01001394 // writable by system, readable by any app within the same user
Robin Lee60fd3fe2014-10-07 16:55:02 +01001395 const int uid = multiuser_get_uid(userid, AID_SYSTEM);
1396 const int gid = multiuser_get_uid(userid, AID_EVERYBODY);
Robin Lee095c7632014-04-25 15:05:19 +01001397
1398 // Ensure /data/misc/user/<userid> exists
Jeff Sharkey379a12b2016-04-14 20:45:06 -06001399 auto path = create_data_misc_legacy_path(userid);
1400 return fs_prepare_dir(path.c_str(), 0750, uid, gid);
Robin Lee095c7632014-04-25 15:05:19 +01001401}
Andreas Gampe02d0de52015-11-11 20:43:16 -08001402
1403int wait_child(pid_t pid)
1404{
1405 int status;
1406 pid_t got_pid;
1407
1408 while (1) {
1409 got_pid = waitpid(pid, &status, 0);
1410 if (got_pid == -1 && errno == EINTR) {
1411 printf("waitpid interrupted, retrying\n");
1412 } else {
1413 break;
1414 }
1415 }
1416 if (got_pid != pid) {
1417 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
1418 (int) pid, (int) got_pid, strerror(errno));
1419 return 1;
1420 }
1421
1422 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1423 return 0;
1424 } else {
1425 return status; /* always nonzero */
1426 }
1427}
1428
Calin Juravle42451c02017-01-17 14:43:25 -08001429/**
1430 * Prepare an app cache directory, which offers to fix-up the GID and
1431 * directory mode flags during a platform upgrade.
1432 * The app cache directory path will be 'parent'/'name'.
1433 */
1434int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
1435 uid_t uid, gid_t gid) {
1436 auto path = StringPrintf("%s/%s", parent.c_str(), name);
1437 struct stat st;
1438 if (stat(path.c_str(), &st) != 0) {
1439 if (errno == ENOENT) {
1440 // This is fine, just create it
1441 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) {
1442 PLOG(ERROR) << "Failed to prepare " << path;
1443 return -1;
1444 } else {
1445 return 0;
1446 }
1447 } else {
1448 PLOG(ERROR) << "Failed to stat " << path;
1449 return -1;
1450 }
1451 }
1452
1453 mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);
1454 if (st.st_uid != uid) {
1455 // Mismatched UID is real trouble; we can't recover
1456 LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid
1457 << " but expected " << uid;
1458 return -1;
1459 } else if (st.st_gid == gid && actual_mode == target_mode) {
1460 // Everything looks good!
1461 return 0;
1462 }
1463
1464 // Directory is owned correctly, but GID or mode mismatch means it's
1465 // probably a platform upgrade so we need to fix them
1466 FTS *fts;
1467 FTSENT *p;
1468 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -07001469 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Calin Juravle42451c02017-01-17 14:43:25 -08001470 PLOG(ERROR) << "Failed to fts_open " << path;
1471 return -1;
1472 }
1473 while ((p = fts_read(fts)) != NULL) {
1474 switch (p->fts_info) {
1475 case FTS_DP:
1476 if (chmod(p->fts_accpath, target_mode) != 0) {
1477 PLOG(WARNING) << "Failed to chmod " << p->fts_path;
1478 }
1479 // Intentional fall through to also set GID
1480 case FTS_F:
1481 if (chown(p->fts_accpath, -1, gid) != 0) {
1482 PLOG(WARNING) << "Failed to chown " << p->fts_path;
1483 }
1484 break;
1485 case FTS_SL:
1486 case FTS_SLNONE:
1487 if (lchown(p->fts_accpath, -1, gid) != 0) {
1488 PLOG(WARNING) << "Failed to chown " << p->fts_path;
1489 }
1490 break;
1491 }
1492 }
1493 fts_close(fts);
1494 return 0;
1495}
1496
Andreas Gampe02d0de52015-11-11 20:43:16 -08001497} // namespace installd
1498} // namespace android