Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2008, The Android Open Source Project |
| 3 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 17 | #include "utils.h" |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 21 | #include <fts.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/wait.h> |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 25 | #include <sys/xattr.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 26 | |
| 27 | #if defined(__APPLE__) |
| 28 | #include <sys/mount.h> |
| 29 | #else |
| 30 | #include <sys/statfs.h> |
| 31 | #endif |
| 32 | |
Elliott Hughes | e4ec9eb | 2015-12-04 15:39:32 -0800 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
| 35 | #include <cutils/fs.h> |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 37 | #include <log/log.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 39 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 40 | #include "globals.h" // extern variables. |
| 41 | |
| 42 | #ifndef LOG_TAG |
| 43 | #define LOG_TAG "installd" |
| 44 | #endif |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 45 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 46 | #define CACHE_NOISY(x) //x |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 47 | #define DEBUG_XATTRS 0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 48 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 49 | using android::base::StringPrintf; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 50 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 51 | namespace android { |
| 52 | namespace installd { |
| 53 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Check that given string is valid filename, and that it attempts no |
| 56 | * parent or child directory traversal. |
| 57 | */ |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 58 | bool is_valid_filename(const std::string& name) { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 59 | if (name.empty() || (name == ".") || (name == "..") |
| 60 | || (name.find('/') != std::string::npos)) { |
| 61 | return false; |
| 62 | } else { |
| 63 | return true; |
| 64 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 67 | static void check_package_name(const char* package_name) { |
| 68 | CHECK(is_valid_filename(package_name)); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 69 | CHECK(is_valid_package_name(package_name)); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 72 | /** |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 73 | * Create the path name where package app contents should be stored for |
| 74 | * the given volume UUID and package name. An empty UUID is assumed to |
| 75 | * be internal storage. |
| 76 | */ |
| 77 | std::string create_data_app_package_path(const char* volume_uuid, |
| 78 | const char* package_name) { |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 79 | check_package_name(package_name); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 80 | return StringPrintf("%s/%s", |
| 81 | create_data_app_path(volume_uuid).c_str(), package_name); |
| 82 | } |
| 83 | |
| 84 | /** |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 85 | * Create the path name where package data should be stored for the given |
| 86 | * volume UUID, package name, and user ID. An empty UUID is assumed to be |
| 87 | * internal storage. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 88 | */ |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 89 | std::string create_data_user_ce_package_path(const char* volume_uuid, |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 90 | userid_t user, const char* package_name) { |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 91 | check_package_name(package_name); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 92 | return StringPrintf("%s/%s", |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 93 | create_data_user_ce_path(volume_uuid, user).c_str(), package_name); |
| 94 | } |
| 95 | |
| 96 | std::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user, |
| 97 | const char* package_name, ino_t ce_data_inode) { |
| 98 | // For testing purposes, rely on the inode when defined; this could be |
| 99 | // optimized to use access() in the future. |
| 100 | auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name); |
| 101 | if (ce_data_inode != 0) { |
| 102 | auto user_path = create_data_user_ce_path(volume_uuid, user); |
| 103 | DIR* dir = opendir(user_path.c_str()); |
| 104 | if (dir == nullptr) { |
| 105 | PLOG(ERROR) << "Failed to opendir " << user_path; |
| 106 | return fallback; |
| 107 | } |
| 108 | |
| 109 | struct dirent* ent; |
| 110 | while ((ent = readdir(dir))) { |
| 111 | if (ent->d_ino == ce_data_inode) { |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 112 | auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name); |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 113 | #if DEBUG_XATTRS |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 114 | if (resolved != fallback) { |
| 115 | LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode |
| 116 | << " instead of " << fallback; |
| 117 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 118 | #endif |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 119 | closedir(dir); |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 120 | return resolved; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 121 | } |
| 122 | } |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 123 | LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 124 | closedir(dir); |
| 125 | return fallback; |
| 126 | } else { |
| 127 | return fallback; |
| 128 | } |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 129 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 130 | |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 131 | std::string create_data_user_de_package_path(const char* volume_uuid, |
| 132 | userid_t user, const char* package_name) { |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 133 | check_package_name(package_name); |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 134 | return StringPrintf("%s/%s", |
| 135 | create_data_user_de_path(volume_uuid, user).c_str(), package_name); |
| 136 | } |
| 137 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 138 | int create_pkg_path(char path[PKG_PATH_MAX], const char *pkgname, |
| 139 | const char *postfix, userid_t userid) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 140 | if (!is_valid_package_name(pkgname)) { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 141 | path[0] = '\0'; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 142 | return -1; |
| 143 | } |
| 144 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 145 | std::string _tmp(create_data_user_ce_package_path(nullptr, userid, pkgname) + postfix); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 146 | const char* tmp = _tmp.c_str(); |
| 147 | if (strlen(tmp) >= PKG_PATH_MAX) { |
| 148 | path[0] = '\0'; |
| 149 | return -1; |
| 150 | } else { |
| 151 | strcpy(path, tmp); |
| 152 | return 0; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 153 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 156 | std::string create_data_path(const char* volume_uuid) { |
| 157 | if (volume_uuid == nullptr) { |
| 158 | return "/data"; |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 159 | } else if (!strcmp(volume_uuid, "TEST")) { |
| 160 | CHECK(property_get_bool("ro.debuggable", false)); |
| 161 | return "/data/local/tmp"; |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 162 | } else { |
| 163 | CHECK(is_valid_filename(volume_uuid)); |
| 164 | return StringPrintf("/mnt/expand/%s", volume_uuid); |
| 165 | } |
| 166 | } |
| 167 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 168 | /** |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 169 | * Create the path name for app data. |
| 170 | */ |
| 171 | std::string create_data_app_path(const char* volume_uuid) { |
| 172 | return StringPrintf("%s/app", create_data_path(volume_uuid).c_str()); |
| 173 | } |
| 174 | |
| 175 | /** |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 176 | * Create the path name for user data for a certain userid. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 177 | */ |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 178 | std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) { |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 179 | std::string data(create_data_path(volume_uuid)); |
| 180 | if (volume_uuid == nullptr) { |
| 181 | if (userid == 0) { |
| 182 | return StringPrintf("%s/data", data.c_str()); |
| 183 | } else { |
| 184 | return StringPrintf("%s/user/%u", data.c_str(), userid); |
| 185 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 186 | } else { |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 187 | return StringPrintf("%s/user/%u", data.c_str(), userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 188 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 192 | * Create the path name for device encrypted user data for a certain userid. |
| 193 | */ |
| 194 | std::string create_data_user_de_path(const char* volume_uuid, userid_t userid) { |
| 195 | std::string data(create_data_path(volume_uuid)); |
| 196 | return StringPrintf("%s/user_de/%u", data.c_str(), userid); |
| 197 | } |
| 198 | |
| 199 | /** |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 200 | * Create the path name for media for a certain userid. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 201 | */ |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 202 | std::string create_data_media_path(const char* volume_uuid, userid_t userid) { |
| 203 | return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 206 | std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) { |
| 207 | return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name); |
| 208 | } |
| 209 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 210 | std::string create_data_media_package_path(const char* volume_uuid, userid_t userid, |
| 211 | const char* data_type, const char* package_name) { |
| 212 | return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(), |
| 213 | data_type, package_name); |
| 214 | } |
| 215 | |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 216 | std::string create_data_misc_legacy_path(userid_t userid) { |
| 217 | return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid); |
| 218 | } |
| 219 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 220 | std::string create_primary_cur_profile_dir_path(userid_t userid) { |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 221 | return StringPrintf("%s/cur/%u", android_profiles_dir.path, userid); |
| 222 | } |
| 223 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 224 | std::string create_primary_current_profile_package_dir_path(userid_t user, |
| 225 | const std::string& package_name) { |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 226 | check_package_name(package_name.c_str()); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 227 | return StringPrintf("%s/%s", |
| 228 | create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str()); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 231 | std::string create_primary_ref_profile_dir_path() { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 232 | return StringPrintf("%s/ref", android_profiles_dir.path); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 235 | std::string create_primary_reference_profile_package_dir_path(const std::string& package_name) { |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 236 | check_package_name(package_name.c_str()); |
| 237 | return StringPrintf("%s/ref/%s", android_profiles_dir.path, package_name.c_str()); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 240 | std::string create_data_dalvik_cache_path() { |
| 241 | return "/data/dalvik-cache"; |
| 242 | } |
| 243 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 244 | // Keep profile paths in sync with ActivityThread and LoadedApk. |
| 245 | const std::string PROFILE_EXT = ".prof"; |
| 246 | const std::string PRIMARY_PROFILE_NAME = "primary" + PROFILE_EXT; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 247 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 248 | std::string create_current_profile_path(userid_t user, const std::string& location, |
| 249 | bool is_secondary_dex) { |
| 250 | if (is_secondary_dex) { |
| 251 | // Secondary dex profiles are stored next to the dex files using .prof extension. |
| 252 | return StringPrintf("%s%s", location.c_str(), PROFILE_EXT.c_str()); |
| 253 | } else { |
| 254 | // Profiles for primary apks are under /data/misc/profiles/cur. |
| 255 | std::string profile_dir = create_primary_current_profile_package_dir_path(user, location); |
| 256 | return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str()); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | std::string create_reference_profile_path(const std::string& location, bool is_secondary_dex) { |
| 261 | if (is_secondary_dex) { |
| 262 | // Secondary dex reference profiles are stored next to the dex files under the oat folder. |
| 263 | size_t dirIndex = location.rfind('/'); |
| 264 | CHECK(dirIndex != std::string::npos) |
| 265 | << "Unexpected dir structure for secondary dex " << location; |
| 266 | |
| 267 | std::string dex_dir = location.substr(0, dirIndex); |
| 268 | std::string dex_name = location.substr(dirIndex +1); |
| 269 | return StringPrintf("%s/oat/%s%s", |
| 270 | dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str()); |
| 271 | } else { |
| 272 | // Reference profiles for primary apks are stored in /data/misc/profile/ref. |
| 273 | std::string profile_dir = create_primary_reference_profile_package_dir_path(location); |
| 274 | return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str()); |
| 275 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 278 | std::vector<userid_t> get_known_users(const char* volume_uuid) { |
| 279 | std::vector<userid_t> users; |
| 280 | |
| 281 | // We always have an owner |
| 282 | users.push_back(0); |
| 283 | |
| 284 | std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX); |
| 285 | DIR* dir = opendir(path.c_str()); |
| 286 | if (dir == NULL) { |
| 287 | // Unable to discover other users, but at least return owner |
| 288 | PLOG(ERROR) << "Failed to opendir " << path; |
| 289 | return users; |
| 290 | } |
| 291 | |
| 292 | struct dirent* ent; |
| 293 | while ((ent = readdir(dir))) { |
| 294 | if (ent->d_type != DT_DIR) { |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | char* end; |
| 299 | userid_t user = strtol(ent->d_name, &end, 10); |
| 300 | if (*end == '\0' && user != 0) { |
| 301 | LOG(DEBUG) << "Found valid user " << user; |
| 302 | users.push_back(user); |
| 303 | } |
| 304 | } |
| 305 | closedir(dir); |
| 306 | |
| 307 | return users; |
| 308 | } |
| 309 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 310 | int calculate_tree_size(const std::string& path, int64_t* size, |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 311 | int32_t include_gid, int32_t exclude_gid, bool exclude_apps) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 312 | FTS *fts; |
| 313 | FTSENT *p; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 314 | int64_t matchedSize = 0; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 315 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 316 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 317 | if (errno != ENOENT) { |
| 318 | PLOG(ERROR) << "Failed to fts_open " << path; |
| 319 | } |
| 320 | return -1; |
| 321 | } |
| 322 | while ((p = fts_read(fts)) != NULL) { |
| 323 | switch (p->fts_info) { |
| 324 | case FTS_D: |
| 325 | case FTS_DEFAULT: |
| 326 | case FTS_F: |
| 327 | case FTS_SL: |
| 328 | case FTS_SLNONE: |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 329 | int32_t uid = p->fts_statp->st_uid; |
| 330 | int32_t gid = p->fts_statp->st_gid; |
| 331 | int32_t user_uid = multiuser_get_app_id(uid); |
| 332 | int32_t user_gid = multiuser_get_app_id(gid); |
| 333 | if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END) |
| 334 | || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END) |
| 335 | || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) { |
| 336 | // Don't traverse inside or measure |
| 337 | fts_set(fts, p, FTS_SKIP); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 338 | break; |
| 339 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 340 | if (include_gid != -1 && gid != include_gid) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 341 | break; |
| 342 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 343 | if (exclude_gid != -1 && gid == exclude_gid) { |
| 344 | break; |
| 345 | } |
| 346 | matchedSize += (p->fts_statp->st_blocks * 512); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 347 | break; |
| 348 | } |
| 349 | } |
| 350 | fts_close(fts); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 351 | #if MEASURE_DEBUG |
| 352 | if ((include_gid == -1) && (exclude_gid == -1)) { |
| 353 | LOG(DEBUG) << "Measured " << path << " size " << matchedSize; |
| 354 | } else { |
| 355 | LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid |
| 356 | << " exclude " << exclude_gid; |
| 357 | } |
| 358 | #endif |
| 359 | *size += matchedSize; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 363 | int create_move_path(char path[PKG_PATH_MAX], |
| 364 | const char* pkgname, |
| 365 | const char* leaf, |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 366 | userid_t userid ATTRIBUTE_UNUSED) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 367 | { |
| 368 | if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1) |
| 369 | >= PKG_PATH_MAX) { |
| 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Checks whether the package name is valid. Returns -1 on error and |
| 379 | * 0 on success. |
| 380 | */ |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 381 | bool is_valid_package_name(const std::string& packageName) { |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 382 | // This logic is borrowed from PackageParser.java |
| 383 | bool hasSep = false; |
| 384 | bool front = true; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 385 | |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 386 | auto it = packageName.begin(); |
| 387 | for (; it != packageName.end() && *it != '-'; it++) { |
| 388 | char c = *it; |
| 389 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { |
| 390 | front = false; |
| 391 | continue; |
| 392 | } |
| 393 | if (!front) { |
| 394 | if ((c >= '0' && c <= '9') || c == '_') { |
| 395 | continue; |
| 396 | } |
| 397 | } |
| 398 | if (c == '.') { |
| 399 | hasSep = true; |
| 400 | front = true; |
| 401 | continue; |
| 402 | } |
| 403 | LOG(WARNING) << "Bad package character " << c << " in " << packageName; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 404 | return false; |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Jeff Sharkey | ab7ac8d | 2017-03-08 12:39:46 -0700 | [diff] [blame] | 407 | if (front) { |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 408 | LOG(WARNING) << "Missing separator in " << packageName; |
| 409 | return false; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 412 | for (; it != packageName.end(); it++) { |
| 413 | char c = *it; |
| 414 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue; |
| 415 | if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue; |
| 416 | LOG(WARNING) << "Bad suffix character " << c << " in " << packageName; |
| 417 | return false; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 420 | return true; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 423 | static int _delete_dir_contents(DIR *d, |
| 424 | int (*exclusion_predicate)(const char *name, const int is_dir)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 425 | { |
| 426 | int result = 0; |
| 427 | struct dirent *de; |
| 428 | int dfd; |
| 429 | |
| 430 | dfd = dirfd(d); |
| 431 | |
| 432 | if (dfd < 0) return -1; |
| 433 | |
| 434 | while ((de = readdir(d))) { |
| 435 | const char *name = de->d_name; |
| 436 | |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 437 | /* check using the exclusion predicate, if provided */ |
| 438 | if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) { |
| 439 | continue; |
| 440 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 441 | |
| 442 | if (de->d_type == DT_DIR) { |
Chih-Hung Hsieh | 99d9fb1 | 2014-09-11 14:44:46 -0700 | [diff] [blame] | 443 | int subfd; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 444 | DIR *subdir; |
| 445 | |
| 446 | /* always skip "." and ".." */ |
| 447 | if (name[0] == '.') { |
| 448 | if (name[1] == 0) continue; |
| 449 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 450 | } |
| 451 | |
Nick Kralevich | 8b7acac | 2015-08-10 13:43:00 -0700 | [diff] [blame] | 452 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 453 | if (subfd < 0) { |
| 454 | ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); |
| 455 | result = -1; |
| 456 | continue; |
| 457 | } |
| 458 | subdir = fdopendir(subfd); |
| 459 | if (subdir == NULL) { |
| 460 | ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); |
| 461 | close(subfd); |
| 462 | result = -1; |
| 463 | continue; |
| 464 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 465 | if (_delete_dir_contents(subdir, exclusion_predicate)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 466 | result = -1; |
| 467 | } |
| 468 | closedir(subdir); |
| 469 | if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { |
| 470 | ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); |
| 471 | result = -1; |
| 472 | } |
| 473 | } else { |
| 474 | if (unlinkat(dfd, name, 0) < 0) { |
| 475 | ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); |
| 476 | result = -1; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return result; |
| 482 | } |
| 483 | |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 484 | int delete_dir_contents(const std::string& pathname, bool ignore_if_missing) { |
| 485 | return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 488 | int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) { |
| 489 | return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 492 | int delete_dir_contents(const char *pathname, |
| 493 | int also_delete_dir, |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 494 | int (*exclusion_predicate)(const char*, const int), |
| 495 | bool ignore_if_missing) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 496 | { |
| 497 | int res = 0; |
| 498 | DIR *d; |
| 499 | |
| 500 | d = opendir(pathname); |
| 501 | if (d == NULL) { |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 502 | if (ignore_if_missing && (errno == ENOENT)) { |
| 503 | return 0; |
| 504 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 505 | ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno)); |
| 506 | return -errno; |
| 507 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 508 | res = _delete_dir_contents(d, exclusion_predicate); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 509 | closedir(d); |
| 510 | if (also_delete_dir) { |
| 511 | if (rmdir(pathname)) { |
| 512 | ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno)); |
| 513 | res = -1; |
| 514 | } |
| 515 | } |
| 516 | return res; |
| 517 | } |
| 518 | |
| 519 | int delete_dir_contents_fd(int dfd, const char *name) |
| 520 | { |
| 521 | int fd, res; |
| 522 | DIR *d; |
| 523 | |
Nick Kralevich | 8b7acac | 2015-08-10 13:43:00 -0700 | [diff] [blame] | 524 | fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 525 | if (fd < 0) { |
| 526 | ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); |
| 527 | return -1; |
| 528 | } |
| 529 | d = fdopendir(fd); |
| 530 | if (d == NULL) { |
| 531 | ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); |
| 532 | close(fd); |
| 533 | return -1; |
| 534 | } |
| 535 | res = _delete_dir_contents(d, 0); |
| 536 | closedir(d); |
| 537 | return res; |
| 538 | } |
| 539 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 540 | static int _copy_owner_permissions(int srcfd, int dstfd) |
| 541 | { |
| 542 | struct stat st; |
| 543 | if (fstat(srcfd, &st) != 0) { |
| 544 | return -1; |
| 545 | } |
| 546 | if (fchmod(dstfd, st.st_mode) != 0) { |
| 547 | return -1; |
| 548 | } |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group) |
| 553 | { |
| 554 | int result = 0; |
| 555 | if (_copy_owner_permissions(sdfd, ddfd) != 0) { |
| 556 | ALOGE("_copy_dir_files failed to copy dir permissions\n"); |
| 557 | } |
| 558 | if (fchown(ddfd, owner, group) != 0) { |
| 559 | ALOGE("_copy_dir_files failed to change dir owner\n"); |
| 560 | } |
| 561 | |
| 562 | DIR *ds = fdopendir(sdfd); |
| 563 | if (ds == NULL) { |
| 564 | ALOGE("Couldn't fdopendir: %s\n", strerror(errno)); |
| 565 | return -1; |
| 566 | } |
| 567 | struct dirent *de; |
| 568 | while ((de = readdir(ds))) { |
| 569 | if (de->d_type != DT_REG) { |
| 570 | continue; |
| 571 | } |
| 572 | |
| 573 | const char *name = de->d_name; |
| 574 | int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); |
| 575 | int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600); |
| 576 | if (fsfd == -1 || fdfd == -1) { |
| 577 | ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); |
| 578 | } else { |
| 579 | if (_copy_owner_permissions(fsfd, fdfd) != 0) { |
| 580 | ALOGE("Failed to change file permissions\n"); |
| 581 | } |
| 582 | if (fchown(fdfd, owner, group) != 0) { |
| 583 | ALOGE("Failed to change file owner\n"); |
| 584 | } |
| 585 | |
| 586 | char buf[8192]; |
| 587 | ssize_t size; |
| 588 | while ((size = read(fsfd, buf, sizeof(buf))) > 0) { |
| 589 | write(fdfd, buf, size); |
| 590 | } |
| 591 | if (size < 0) { |
| 592 | ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); |
| 593 | result = -1; |
| 594 | } |
| 595 | } |
| 596 | close(fdfd); |
| 597 | close(fsfd); |
| 598 | } |
| 599 | |
| 600 | return result; |
| 601 | } |
| 602 | |
| 603 | int copy_dir_files(const char *srcname, |
| 604 | const char *dstname, |
| 605 | uid_t owner, |
| 606 | uid_t group) |
| 607 | { |
| 608 | int res = 0; |
| 609 | DIR *ds = NULL; |
| 610 | DIR *dd = NULL; |
| 611 | |
| 612 | ds = opendir(srcname); |
| 613 | if (ds == NULL) { |
| 614 | ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno)); |
| 615 | return -errno; |
| 616 | } |
| 617 | |
| 618 | mkdir(dstname, 0600); |
| 619 | dd = opendir(dstname); |
| 620 | if (dd == NULL) { |
| 621 | ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno)); |
| 622 | closedir(ds); |
| 623 | return -errno; |
| 624 | } |
| 625 | |
| 626 | int sdfd = dirfd(ds); |
| 627 | int ddfd = dirfd(dd); |
| 628 | if (sdfd != -1 && ddfd != -1) { |
| 629 | res = _copy_dir_files(sdfd, ddfd, owner, group); |
| 630 | } else { |
| 631 | res = -errno; |
| 632 | } |
| 633 | closedir(dd); |
| 634 | closedir(ds); |
| 635 | return res; |
| 636 | } |
| 637 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 638 | int64_t data_disk_free(const std::string& data_path) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 639 | { |
| 640 | struct statfs sfs; |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 641 | if (statfs(data_path.c_str(), &sfs) == 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 642 | return sfs.f_bavail * sfs.f_bsize; |
| 643 | } else { |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 644 | PLOG(ERROR) << "Couldn't statfs " << data_path; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 645 | return -1; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | cache_t* start_cache_collection() |
| 650 | { |
| 651 | cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t)); |
| 652 | return cache; |
| 653 | } |
| 654 | |
| 655 | #define CACHE_BLOCK_SIZE (512*1024) |
| 656 | |
| 657 | static void* _cache_malloc(cache_t* cache, size_t len) |
| 658 | { |
| 659 | len = (len+3)&~3; |
| 660 | if (len > (CACHE_BLOCK_SIZE/2)) { |
| 661 | // It doesn't make sense to try to put this allocation into one |
| 662 | // of our blocks, because it is so big. Instead, make a new dedicated |
| 663 | // block for it. |
| 664 | int8_t* res = (int8_t*)malloc(len+sizeof(void*)); |
| 665 | if (res == NULL) { |
| 666 | return NULL; |
| 667 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 668 | CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %zu", res, len)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 669 | // Link it into our list of blocks, not disrupting the current one. |
| 670 | if (cache->memBlocks == NULL) { |
| 671 | *(void**)res = NULL; |
| 672 | cache->memBlocks = res; |
| 673 | } else { |
| 674 | *(void**)res = *(void**)cache->memBlocks; |
| 675 | *(void**)cache->memBlocks = res; |
| 676 | } |
| 677 | return res + sizeof(void*); |
| 678 | } |
| 679 | int8_t* res = cache->curMemBlockAvail; |
| 680 | int8_t* nextPos = res + len; |
| 681 | if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) { |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 682 | int8_t* newBlock = (int8_t*) malloc(CACHE_BLOCK_SIZE); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 683 | if (newBlock == NULL) { |
| 684 | return NULL; |
| 685 | } |
| 686 | CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock)); |
| 687 | *(void**)newBlock = cache->memBlocks; |
| 688 | cache->memBlocks = newBlock; |
| 689 | res = cache->curMemBlockAvail = newBlock + sizeof(void*); |
| 690 | cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE; |
| 691 | nextPos = res + len; |
| 692 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 693 | CACHE_NOISY(ALOGI("cache_malloc: ret %p size %zu, block=%p, nextPos=%p", |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 694 | res, len, cache->memBlocks, nextPos)); |
| 695 | cache->curMemBlockAvail = nextPos; |
| 696 | return res; |
| 697 | } |
| 698 | |
| 699 | static void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len) |
| 700 | { |
| 701 | // This isn't really a realloc, but it is good enough for our purposes here. |
| 702 | void* alloc = _cache_malloc(cache, len); |
| 703 | if (alloc != NULL && cur != NULL) { |
| 704 | memcpy(alloc, cur, origLen < len ? origLen : len); |
| 705 | } |
| 706 | return alloc; |
| 707 | } |
| 708 | |
| 709 | static void _inc_num_cache_collected(cache_t* cache) |
| 710 | { |
| 711 | cache->numCollected++; |
| 712 | if ((cache->numCollected%20000) == 0) { |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 713 | ALOGI("Collected cache so far: %zd directories, %zd files", |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 714 | cache->numDirs, cache->numFiles); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | static cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name) |
| 719 | { |
| 720 | size_t nameLen = strlen(name); |
| 721 | cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1); |
| 722 | if (dir != NULL) { |
| 723 | dir->parent = parent; |
| 724 | dir->childCount = 0; |
| 725 | dir->hiddenCount = 0; |
| 726 | dir->deleted = 0; |
| 727 | strcpy(dir->name, name); |
| 728 | if (cache->numDirs >= cache->availDirs) { |
| 729 | size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2; |
| 730 | cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs, |
| 731 | cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*)); |
| 732 | if (newDirs == NULL) { |
| 733 | ALOGE("Failure growing cache dirs array for %s\n", name); |
| 734 | return NULL; |
| 735 | } |
| 736 | cache->availDirs = newAvail; |
| 737 | cache->dirs = newDirs; |
| 738 | } |
| 739 | cache->dirs[cache->numDirs] = dir; |
| 740 | cache->numDirs++; |
| 741 | if (parent != NULL) { |
| 742 | parent->childCount++; |
| 743 | } |
| 744 | _inc_num_cache_collected(cache); |
| 745 | } else { |
| 746 | ALOGE("Failure allocating cache_dir_t for %s\n", name); |
| 747 | } |
| 748 | return dir; |
| 749 | } |
| 750 | |
| 751 | static cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime, |
| 752 | const char *name) |
| 753 | { |
| 754 | size_t nameLen = strlen(name); |
| 755 | cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1); |
| 756 | if (file != NULL) { |
| 757 | file->dir = dir; |
| 758 | file->modTime = modTime; |
| 759 | strcpy(file->name, name); |
| 760 | if (cache->numFiles >= cache->availFiles) { |
| 761 | size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2; |
| 762 | cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files, |
| 763 | cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*)); |
| 764 | if (newFiles == NULL) { |
| 765 | ALOGE("Failure growing cache file array for %s\n", name); |
| 766 | return NULL; |
| 767 | } |
| 768 | cache->availFiles = newAvail; |
| 769 | cache->files = newFiles; |
| 770 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 771 | CACHE_NOISY(ALOGI("Setting file %p at position %zd in array %p", file, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 772 | cache->numFiles, cache->files)); |
| 773 | cache->files[cache->numFiles] = file; |
| 774 | cache->numFiles++; |
| 775 | dir->childCount++; |
| 776 | _inc_num_cache_collected(cache); |
| 777 | } else { |
| 778 | ALOGE("Failure allocating cache_file_t for %s\n", name); |
| 779 | } |
| 780 | return file; |
| 781 | } |
| 782 | |
| 783 | static int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName, |
| 784 | DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen) |
| 785 | { |
| 786 | struct dirent *de; |
| 787 | cache_dir_t* cacheDir = NULL; |
| 788 | int dfd; |
| 789 | |
| 790 | CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s", |
| 791 | parentDir, dirName, dir, pathBase)); |
| 792 | |
| 793 | dfd = dirfd(dir); |
| 794 | |
| 795 | if (dfd < 0) return 0; |
| 796 | |
| 797 | // Sub-directories always get added to the data structure, so if they |
| 798 | // are empty we will know about them to delete them later. |
| 799 | cacheDir = _add_cache_dir_t(cache, parentDir, dirName); |
| 800 | |
| 801 | while ((de = readdir(dir))) { |
| 802 | const char *name = de->d_name; |
| 803 | |
| 804 | if (de->d_type == DT_DIR) { |
| 805 | int subfd; |
| 806 | DIR *subdir; |
| 807 | |
| 808 | /* always skip "." and ".." */ |
| 809 | if (name[0] == '.') { |
| 810 | if (name[1] == 0) continue; |
| 811 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 812 | } |
| 813 | |
Nick Kralevich | 8b7acac | 2015-08-10 13:43:00 -0700 | [diff] [blame] | 814 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 815 | if (subfd < 0) { |
| 816 | ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); |
| 817 | continue; |
| 818 | } |
| 819 | subdir = fdopendir(subfd); |
| 820 | if (subdir == NULL) { |
| 821 | ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); |
| 822 | close(subfd); |
| 823 | continue; |
| 824 | } |
| 825 | if (cacheDir == NULL) { |
| 826 | cacheDir = _add_cache_dir_t(cache, parentDir, dirName); |
| 827 | } |
| 828 | if (cacheDir != NULL) { |
| 829 | // Update pathBase for the new path... this may change dirName |
| 830 | // if that is also pointing to the path, but we are done with it |
| 831 | // now. |
| 832 | size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name); |
| 833 | CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase)); |
| 834 | if (finallen < pathAvailLen) { |
| 835 | _add_cache_files(cache, cacheDir, name, subdir, pathBase, |
| 836 | pathPos+finallen, pathAvailLen-finallen); |
| 837 | } else { |
| 838 | // Whoops, the final path is too long! We'll just delete |
| 839 | // this directory. |
| 840 | ALOGW("Cache dir %s truncated in path %s; deleting dir\n", |
| 841 | name, pathBase); |
| 842 | _delete_dir_contents(subdir, NULL); |
| 843 | if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { |
| 844 | ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | closedir(subdir); |
| 849 | } else if (de->d_type == DT_REG) { |
| 850 | // Skip files that start with '.'; they will be deleted if |
| 851 | // their entire directory is deleted. This allows for metadata |
| 852 | // like ".nomedia" to remain in the directory until the entire |
| 853 | // directory is deleted. |
| 854 | if (cacheDir == NULL) { |
| 855 | cacheDir = _add_cache_dir_t(cache, parentDir, dirName); |
| 856 | } |
| 857 | if (name[0] == '.') { |
| 858 | cacheDir->hiddenCount++; |
| 859 | continue; |
| 860 | } |
| 861 | if (cacheDir != NULL) { |
| 862 | // Build final full path for file... this may change dirName |
| 863 | // if that is also pointing to the path, but we are done with it |
| 864 | // now. |
| 865 | size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name); |
| 866 | CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase)); |
| 867 | if (finallen < pathAvailLen) { |
| 868 | struct stat s; |
| 869 | if (stat(pathBase, &s) >= 0) { |
| 870 | _add_cache_file_t(cache, cacheDir, s.st_mtime, name); |
| 871 | } else { |
| 872 | ALOGW("Unable to stat cache file %s; deleting\n", pathBase); |
| 873 | if (unlink(pathBase) < 0) { |
| 874 | ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno)); |
| 875 | } |
| 876 | } |
| 877 | } else { |
| 878 | // Whoops, the final path is too long! We'll just delete |
| 879 | // this file. |
| 880 | ALOGW("Cache file %s truncated in path %s; deleting\n", |
| 881 | name, pathBase); |
| 882 | if (unlinkat(dfd, name, 0) < 0) { |
| 883 | *pathPos = 0; |
| 884 | ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase, |
| 885 | strerror(errno)); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | } else { |
| 890 | cacheDir->hiddenCount++; |
| 891 | } |
| 892 | } |
| 893 | return 0; |
| 894 | } |
| 895 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 896 | int get_path_inode(const std::string& path, ino_t *inode) { |
| 897 | struct stat buf; |
| 898 | memset(&buf, 0, sizeof(buf)); |
| 899 | if (stat(path.c_str(), &buf) != 0) { |
| 900 | PLOG(WARNING) << "Failed to stat " << path; |
| 901 | return -1; |
| 902 | } else { |
| 903 | *inode = buf.st_ino; |
| 904 | return 0; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * Write the inode of a specific child file into the given xattr on the |
| 910 | * parent directory. This allows you to find the child later, even if its |
| 911 | * name is encrypted. |
| 912 | */ |
| 913 | int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { |
| 914 | ino_t inode = 0; |
| 915 | uint64_t inode_raw = 0; |
| 916 | auto path = StringPrintf("%s/%s", parent.c_str(), name); |
| 917 | |
| 918 | if (get_path_inode(path, &inode) != 0) { |
| 919 | // Path probably doesn't exist yet; ignore |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | // Check to see if already set correctly |
| 924 | if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { |
| 925 | if (inode_raw == inode) { |
| 926 | // Already set correctly; skip writing |
| 927 | return 0; |
| 928 | } else { |
| 929 | PLOG(WARNING) << "Mismatched inode value; found " << inode |
| 930 | << " on disk but marked value was " << inode_raw << "; overwriting"; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | inode_raw = inode; |
Jeff Sharkey | 4ed6507 | 2016-07-22 11:38:54 -0600 | [diff] [blame] | 935 | if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) { |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 936 | PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent; |
| 937 | return -1; |
| 938 | } else { |
| 939 | return 0; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * Read the inode of a specific child file from the given xattr on the |
| 945 | * parent directory. Returns a currently valid path for that child, which |
| 946 | * might have an encrypted name. |
| 947 | */ |
| 948 | std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { |
| 949 | ino_t inode = 0; |
| 950 | uint64_t inode_raw = 0; |
| 951 | auto fallback = StringPrintf("%s/%s", parent.c_str(), name); |
| 952 | |
| 953 | // Lookup the inode value written earlier |
| 954 | if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { |
| 955 | inode = inode_raw; |
| 956 | } |
| 957 | |
| 958 | // For testing purposes, rely on the inode when defined; this could be |
| 959 | // optimized to use access() in the future. |
| 960 | if (inode != 0) { |
| 961 | DIR* dir = opendir(parent.c_str()); |
| 962 | if (dir == nullptr) { |
| 963 | PLOG(ERROR) << "Failed to opendir " << parent; |
| 964 | return fallback; |
| 965 | } |
| 966 | |
| 967 | struct dirent* ent; |
| 968 | while ((ent = readdir(dir))) { |
| 969 | if (ent->d_ino == inode) { |
| 970 | auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name); |
| 971 | #if DEBUG_XATTRS |
| 972 | if (resolved != fallback) { |
| 973 | LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode |
| 974 | << " instead of " << fallback; |
| 975 | } |
| 976 | #endif |
| 977 | closedir(dir); |
| 978 | return resolved; |
| 979 | } |
| 980 | } |
| 981 | LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback; |
| 982 | closedir(dir); |
| 983 | return fallback; |
| 984 | } else { |
| 985 | return fallback; |
| 986 | } |
| 987 | } |
| 988 | |
Jeff Sharkey | 54e292e | 2016-05-10 17:21:13 -0600 | [diff] [blame] | 989 | void add_cache_files(cache_t* cache, const std::string& data_path) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 990 | DIR *d; |
| 991 | struct dirent *de; |
| 992 | char dirname[PATH_MAX]; |
| 993 | |
Jeff Sharkey | 54e292e | 2016-05-10 17:21:13 -0600 | [diff] [blame] | 994 | const char* basepath = data_path.c_str(); |
| 995 | CACHE_NOISY(ALOGI("add_cache_files: basepath=%s\n", basepath)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 996 | |
| 997 | d = opendir(basepath); |
| 998 | if (d == NULL) { |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | while ((de = readdir(d))) { |
| 1003 | if (de->d_type == DT_DIR) { |
| 1004 | DIR* subdir; |
| 1005 | const char *name = de->d_name; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1006 | |
| 1007 | /* always skip "." and ".." */ |
| 1008 | if (name[0] == '.') { |
| 1009 | if (name[1] == 0) continue; |
| 1010 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 1011 | } |
| 1012 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 1013 | auto parent = StringPrintf("%s/%s", basepath, name); |
| 1014 | auto resolved = read_path_inode(parent, "cache", kXattrInodeCache); |
| 1015 | strcpy(dirname, resolved.c_str()); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1016 | CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname)); |
Jeff Sharkey | 54e292e | 2016-05-10 17:21:13 -0600 | [diff] [blame] | 1017 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1018 | subdir = opendir(dirname); |
| 1019 | if (subdir != NULL) { |
| 1020 | size_t dirnameLen = strlen(dirname); |
| 1021 | _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen, |
| 1022 | PATH_MAX - dirnameLen); |
| 1023 | closedir(subdir); |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | closedir(d); |
| 1029 | } |
| 1030 | |
Fyodor Kupolov | f3124d8 | 2016-09-07 13:07:45 -0700 | [diff] [blame] | 1031 | void add_preloads_file_cache(cache_t* cache, const char* volume_uuid) { |
| 1032 | char dirname[PATH_MAX]; |
| 1033 | DIR* subdir; |
| 1034 | auto cache_path = StringPrintf("%s/preloads/file_cache", create_data_path(volume_uuid).c_str()); |
| 1035 | strcpy(dirname, cache_path.c_str()); |
| 1036 | CACHE_NOISY(ALOGI("add_preloads_file_cache: dirname=%s\n", dirname)); |
| 1037 | subdir = opendir(dirname); |
| 1038 | if (subdir != NULL) { |
| 1039 | size_t dirnameLen = strlen(dirname); |
| 1040 | _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname + dirnameLen, |
| 1041 | PATH_MAX - dirnameLen); |
| 1042 | closedir(subdir); |
| 1043 | } |
| 1044 | } |
| 1045 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1046 | static char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir) |
| 1047 | { |
| 1048 | char *pos = path; |
| 1049 | if (dir->parent != NULL) { |
| 1050 | pos = create_dir_path(path, dir->parent); |
| 1051 | } |
| 1052 | // Note that we don't need to worry about going beyond the buffer, |
| 1053 | // since when we were constructing the cache entries our maximum |
| 1054 | // buffer size for full paths was PATH_MAX. |
| 1055 | strcpy(pos, dir->name); |
| 1056 | pos += strlen(pos); |
| 1057 | *pos = '/'; |
| 1058 | pos++; |
| 1059 | *pos = 0; |
| 1060 | return pos; |
| 1061 | } |
| 1062 | |
| 1063 | static void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir) |
| 1064 | { |
| 1065 | if (dir->parent != NULL) { |
| 1066 | create_dir_path(path, dir); |
| 1067 | ALOGI("DEL DIR %s\n", path); |
| 1068 | if (dir->hiddenCount <= 0) { |
| 1069 | if (rmdir(path)) { |
| 1070 | ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno)); |
| 1071 | return; |
| 1072 | } |
| 1073 | } else { |
| 1074 | // The directory contains hidden files so we need to delete |
| 1075 | // them along with the directory itself. |
| 1076 | if (delete_dir_contents(path, 1, NULL)) { |
| 1077 | return; |
| 1078 | } |
| 1079 | } |
| 1080 | dir->parent->childCount--; |
| 1081 | dir->deleted = 1; |
| 1082 | if (dir->parent->childCount <= 0) { |
| 1083 | delete_cache_dir(path, dir->parent); |
| 1084 | } |
| 1085 | } else if (dir->hiddenCount > 0) { |
| 1086 | // This is a root directory, but it has hidden files. Get rid of |
| 1087 | // all of those files, but not the directory itself. |
| 1088 | create_dir_path(path, dir); |
| 1089 | ALOGI("DEL CONTENTS %s\n", path); |
| 1090 | delete_dir_contents(path, 0, NULL); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | static int cache_modtime_sort(const void *lhsP, const void *rhsP) |
| 1095 | { |
| 1096 | const cache_file_t *lhs = *(const cache_file_t**)lhsP; |
| 1097 | const cache_file_t *rhs = *(const cache_file_t**)rhsP; |
| 1098 | return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0); |
| 1099 | } |
| 1100 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1101 | void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1102 | { |
| 1103 | size_t i; |
| 1104 | int skip = 0; |
| 1105 | char path[PATH_MAX]; |
| 1106 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 1107 | ALOGI("Collected cache files: %zd directories, %zd files", |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1108 | cache->numDirs, cache->numFiles); |
| 1109 | |
| 1110 | CACHE_NOISY(ALOGI("Sorting files...")); |
| 1111 | qsort(cache->files, cache->numFiles, sizeof(cache_file_t*), |
| 1112 | cache_modtime_sort); |
| 1113 | |
| 1114 | CACHE_NOISY(ALOGI("Cleaning empty directories...")); |
| 1115 | for (i=cache->numDirs; i>0; i--) { |
| 1116 | cache_dir_t* dir = cache->dirs[i-1]; |
| 1117 | if (dir->childCount <= 0 && !dir->deleted) { |
| 1118 | delete_cache_dir(path, dir); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | CACHE_NOISY(ALOGI("Trimming files...")); |
| 1123 | for (i=0; i<cache->numFiles; i++) { |
| 1124 | skip++; |
| 1125 | if (skip > 10) { |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1126 | if (data_disk_free(data_path) > free_size) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1127 | return; |
| 1128 | } |
| 1129 | skip = 0; |
| 1130 | } |
| 1131 | cache_file_t* file = cache->files[i]; |
| 1132 | strcpy(create_dir_path(path, file->dir), file->name); |
| 1133 | ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path); |
| 1134 | if (unlink(path) < 0) { |
| 1135 | ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno)); |
| 1136 | } |
| 1137 | file->dir->childCount--; |
| 1138 | if (file->dir->childCount <= 0) { |
| 1139 | delete_cache_dir(path, file->dir); |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | void finish_cache_collection(cache_t* cache) |
| 1145 | { |
Chih-Hung Hsieh | 99d9fb1 | 2014-09-11 14:44:46 -0700 | [diff] [blame] | 1146 | CACHE_NOISY(size_t i;) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1147 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 1148 | CACHE_NOISY(ALOGI("clear_cache_files: %zu dirs, %zu files\n", cache->numDirs, cache->numFiles)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1149 | CACHE_NOISY( |
| 1150 | for (i=0; i<cache->numDirs; i++) { |
| 1151 | cache_dir_t* dir = cache->dirs[i]; |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 1152 | ALOGI("dir #%zu: %p %s parent=%p\n", i, dir, dir->name, dir->parent); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1153 | }) |
| 1154 | CACHE_NOISY( |
| 1155 | for (i=0; i<cache->numFiles; i++) { |
| 1156 | cache_file_t* file = cache->files[i]; |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 1157 | ALOGI("file #%zu: %p %s time=%d dir=%p\n", i, file, file->name, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1158 | (int)file->modTime, file->dir); |
| 1159 | }) |
| 1160 | void* block = cache->memBlocks; |
| 1161 | while (block != NULL) { |
| 1162 | void* nextBlock = *(void**)block; |
| 1163 | CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block)); |
| 1164 | free(block); |
| 1165 | block = nextBlock; |
| 1166 | } |
| 1167 | free(cache); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 1171 | * Validate that the path is valid in the context of the provided directory. |
| 1172 | * The path is allowed to have at most one subdirectory and no indirections |
| 1173 | * to top level directories (i.e. have ".."). |
| 1174 | */ |
Jeff Sharkey | e23a132 | 2015-04-06 16:19:39 -0700 | [diff] [blame] | 1175 | static int validate_path(const dir_rec_t* dir, const char* path, int maxSubdirs) { |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 1176 | size_t dir_len = dir->len; |
| 1177 | const char* subdir = strchr(path + dir_len, '/'); |
| 1178 | |
| 1179 | // Only allow the path to have at most one subdirectory. |
| 1180 | if (subdir != NULL) { |
| 1181 | ++subdir; |
Jeff Sharkey | e23a132 | 2015-04-06 16:19:39 -0700 | [diff] [blame] | 1182 | if ((--maxSubdirs == 0) && strchr(subdir, '/') != NULL) { |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 1183 | ALOGE("invalid apk path '%s' (subdir?)\n", path); |
| 1184 | return -1; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | // Directories can't have a period directly after the directory markers to prevent "..". |
| 1189 | if ((path[dir_len] == '.') || ((subdir != NULL) && (*subdir == '.'))) { |
| 1190 | ALOGE("invalid apk path '%s' (trickery)\n", path); |
| 1191 | return -1; |
| 1192 | } |
| 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
| 1197 | /** |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1198 | * Checks whether a path points to a system app (.apk file). Returns 0 |
| 1199 | * if it is a system app or -1 if it is not. |
| 1200 | */ |
| 1201 | int validate_system_app_path(const char* path) { |
| 1202 | size_t i; |
| 1203 | |
| 1204 | for (i = 0; i < android_system_dirs.count; i++) { |
| 1205 | const size_t dir_len = android_system_dirs.dirs[i].len; |
| 1206 | if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) { |
Jeff Sharkey | e23a132 | 2015-04-06 16:19:39 -0700 | [diff] [blame] | 1207 | return validate_path(android_system_dirs.dirs + i, path, 1); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | return -1; |
| 1212 | } |
| 1213 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1214 | bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path, |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1215 | const char* volume_uuid, int uid, int storage_flag) { |
| 1216 | CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE); |
| 1217 | |
| 1218 | std::string app_private_dir = storage_flag == FLAG_STORAGE_CE |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1219 | ? create_data_user_ce_package_path( |
| 1220 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()) |
| 1221 | : create_data_user_de_package_path( |
| 1222 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()); |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1223 | dir_rec_t dir; |
| 1224 | if (get_path_from_string(&dir, app_private_dir.c_str()) != 0) { |
| 1225 | LOG(WARNING) << "Could not get dir rec for " << app_private_dir; |
| 1226 | return false; |
| 1227 | } |
| 1228 | // Usually secondary dex files have a nested directory structure. |
| 1229 | // Pick at most 10 subdirectories when validating (arbitrary value). |
| 1230 | // If the secondary dex file is >10 directory nested then validation will |
| 1231 | // fail and the file will not be compiled. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1232 | return validate_path(&dir, dex_path.c_str(), /*max_subdirs*/ 10) == 0; |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1235 | /** |
| 1236 | * Get the contents of a environment variable that contains a path. Caller |
| 1237 | * owns the string that is inserted into the directory record. Returns |
| 1238 | * 0 on success and -1 on error. |
| 1239 | */ |
| 1240 | int get_path_from_env(dir_rec_t* rec, const char* var) { |
| 1241 | const char* path = getenv(var); |
| 1242 | int ret = get_path_from_string(rec, path); |
| 1243 | if (ret < 0) { |
| 1244 | ALOGW("Problem finding value for environment variable %s\n", var); |
| 1245 | } |
| 1246 | return ret; |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * Puts the string into the record as a directory. Appends '/' to the end |
| 1251 | * of all paths. Caller owns the string that is inserted into the directory |
| 1252 | * record. A null value will result in an error. |
| 1253 | * |
| 1254 | * Returns 0 on success and -1 on error. |
| 1255 | */ |
| 1256 | int get_path_from_string(dir_rec_t* rec, const char* path) { |
| 1257 | if (path == NULL) { |
| 1258 | return -1; |
| 1259 | } else { |
| 1260 | const size_t path_len = strlen(path); |
| 1261 | if (path_len <= 0) { |
| 1262 | return -1; |
| 1263 | } |
| 1264 | |
| 1265 | // Make sure path is absolute. |
| 1266 | if (path[0] != '/') { |
| 1267 | return -1; |
| 1268 | } |
| 1269 | |
| 1270 | if (path[path_len - 1] == '/') { |
| 1271 | // Path ends with a forward slash. Make our own copy. |
| 1272 | |
| 1273 | rec->path = strdup(path); |
| 1274 | if (rec->path == NULL) { |
| 1275 | return -1; |
| 1276 | } |
| 1277 | |
| 1278 | rec->len = path_len; |
| 1279 | } else { |
| 1280 | // Path does not end with a slash. Generate a new string. |
| 1281 | char *dst; |
| 1282 | |
| 1283 | // Add space for slash and terminating null. |
| 1284 | size_t dst_size = path_len + 2; |
| 1285 | |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 1286 | rec->path = (char*) malloc(dst_size); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1287 | if (rec->path == NULL) { |
| 1288 | return -1; |
| 1289 | } |
| 1290 | |
| 1291 | dst = rec->path; |
| 1292 | |
| 1293 | if (append_and_increment(&dst, path, &dst_size) < 0 |
| 1294 | || append_and_increment(&dst, "/", &dst_size)) { |
| 1295 | ALOGE("Error canonicalizing path"); |
| 1296 | return -1; |
| 1297 | } |
| 1298 | |
| 1299 | rec->len = dst - rec->path; |
| 1300 | } |
| 1301 | } |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) { |
| 1306 | dst->len = src->len + strlen(suffix); |
| 1307 | const size_t dstSize = dst->len + 1; |
| 1308 | dst->path = (char*) malloc(dstSize); |
| 1309 | |
| 1310 | if (dst->path == NULL |
| 1311 | || snprintf(dst->path, dstSize, "%s%s", src->path, suffix) |
| 1312 | != (ssize_t) dst->len) { |
| 1313 | ALOGE("Could not allocate memory to hold appended path; aborting\n"); |
| 1314 | return -1; |
| 1315 | } |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | /** |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1321 | * Check whether path points to a valid path for an APK file. The path must |
| 1322 | * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within |
| 1323 | * that path. Returns -1 when an invalid path is encountered and 0 when a valid path |
| 1324 | * is encountered. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1325 | */ |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1326 | static int validate_apk_path_internal(const char *path, int maxSubdirs) { |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 1327 | const dir_rec_t* dir = NULL; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1328 | if (!strncmp(path, android_app_dir.path, android_app_dir.len)) { |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 1329 | dir = &android_app_dir; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1330 | } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) { |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 1331 | dir = &android_app_private_dir; |
Todd Kennedy | 5c1a910 | 2015-11-23 15:18:10 -0800 | [diff] [blame] | 1332 | } else if (!strncmp(path, android_app_ephemeral_dir.path, android_app_ephemeral_dir.len)) { |
| 1333 | dir = &android_app_ephemeral_dir; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1334 | } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) { |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 1335 | dir = &android_asec_dir; |
Jeff Sharkey | e23a132 | 2015-04-06 16:19:39 -0700 | [diff] [blame] | 1336 | } else if (!strncmp(path, android_mnt_expand_dir.path, android_mnt_expand_dir.len)) { |
| 1337 | dir = &android_mnt_expand_dir; |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1338 | if (maxSubdirs < 2) { |
| 1339 | maxSubdirs = 2; |
| 1340 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1341 | } else { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1342 | return -1; |
| 1343 | } |
| 1344 | |
Jeff Sharkey | e23a132 | 2015-04-06 16:19:39 -0700 | [diff] [blame] | 1345 | return validate_path(dir, path, maxSubdirs); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1348 | int validate_apk_path(const char* path) { |
| 1349 | return validate_apk_path_internal(path, 1 /* maxSubdirs */); |
| 1350 | } |
| 1351 | |
| 1352 | int validate_apk_path_subdirs(const char* path) { |
| 1353 | return validate_apk_path_internal(path, 3 /* maxSubdirs */); |
| 1354 | } |
| 1355 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1356 | int append_and_increment(char** dst, const char* src, size_t* dst_size) { |
| 1357 | ssize_t ret = strlcpy(*dst, src, *dst_size); |
| 1358 | if (ret < 0 || (size_t) ret >= *dst_size) { |
| 1359 | return -1; |
| 1360 | } |
| 1361 | *dst += ret; |
| 1362 | *dst_size -= ret; |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 1366 | char *build_string2(const char *s1, const char *s2) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1367 | if (s1 == NULL || s2 == NULL) return NULL; |
| 1368 | |
| 1369 | int len_s1 = strlen(s1); |
| 1370 | int len_s2 = strlen(s2); |
| 1371 | int len = len_s1 + len_s2 + 1; |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 1372 | char *result = (char *) malloc(len); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1373 | if (result == NULL) return NULL; |
| 1374 | |
| 1375 | strcpy(result, s1); |
| 1376 | strcpy(result + len_s1, s2); |
| 1377 | |
| 1378 | return result; |
| 1379 | } |
| 1380 | |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 1381 | char *build_string3(const char *s1, const char *s2, const char *s3) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1382 | if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL; |
| 1383 | |
| 1384 | int len_s1 = strlen(s1); |
| 1385 | int len_s2 = strlen(s2); |
| 1386 | int len_s3 = strlen(s3); |
| 1387 | int len = len_s1 + len_s2 + len_s3 + 1; |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 1388 | char *result = (char *) malloc(len); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1389 | if (result == NULL) return NULL; |
| 1390 | |
| 1391 | strcpy(result, s1); |
| 1392 | strcpy(result + len_s1, s2); |
| 1393 | strcpy(result + len_s1 + len_s2, s3); |
| 1394 | |
| 1395 | return result; |
| 1396 | } |
| 1397 | |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 1398 | int ensure_config_user_dirs(userid_t userid) { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 1399 | // writable by system, readable by any app within the same user |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 1400 | const int uid = multiuser_get_uid(userid, AID_SYSTEM); |
| 1401 | const int gid = multiuser_get_uid(userid, AID_EVERYBODY); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 1402 | |
| 1403 | // Ensure /data/misc/user/<userid> exists |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 1404 | auto path = create_data_misc_legacy_path(userid); |
| 1405 | return fs_prepare_dir(path.c_str(), 0750, uid, gid); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 1406 | } |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 1407 | |
| 1408 | int wait_child(pid_t pid) |
| 1409 | { |
| 1410 | int status; |
| 1411 | pid_t got_pid; |
| 1412 | |
| 1413 | while (1) { |
| 1414 | got_pid = waitpid(pid, &status, 0); |
| 1415 | if (got_pid == -1 && errno == EINTR) { |
| 1416 | printf("waitpid interrupted, retrying\n"); |
| 1417 | } else { |
| 1418 | break; |
| 1419 | } |
| 1420 | } |
| 1421 | if (got_pid != pid) { |
| 1422 | ALOGW("waitpid failed: wanted %d, got %d: %s\n", |
| 1423 | (int) pid, (int) got_pid, strerror(errno)); |
| 1424 | return 1; |
| 1425 | } |
| 1426 | |
| 1427 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 1428 | return 0; |
| 1429 | } else { |
| 1430 | return status; /* always nonzero */ |
| 1431 | } |
| 1432 | } |
| 1433 | |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1434 | /** |
| 1435 | * Prepare an app cache directory, which offers to fix-up the GID and |
| 1436 | * directory mode flags during a platform upgrade. |
| 1437 | * The app cache directory path will be 'parent'/'name'. |
| 1438 | */ |
| 1439 | int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, |
| 1440 | uid_t uid, gid_t gid) { |
| 1441 | auto path = StringPrintf("%s/%s", parent.c_str(), name); |
| 1442 | struct stat st; |
| 1443 | if (stat(path.c_str(), &st) != 0) { |
| 1444 | if (errno == ENOENT) { |
| 1445 | // This is fine, just create it |
| 1446 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { |
| 1447 | PLOG(ERROR) << "Failed to prepare " << path; |
| 1448 | return -1; |
| 1449 | } else { |
| 1450 | return 0; |
| 1451 | } |
| 1452 | } else { |
| 1453 | PLOG(ERROR) << "Failed to stat " << path; |
| 1454 | return -1; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); |
| 1459 | if (st.st_uid != uid) { |
| 1460 | // Mismatched UID is real trouble; we can't recover |
| 1461 | LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid |
| 1462 | << " but expected " << uid; |
| 1463 | return -1; |
| 1464 | } else if (st.st_gid == gid && actual_mode == target_mode) { |
| 1465 | // Everything looks good! |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | // Directory is owned correctly, but GID or mode mismatch means it's |
| 1470 | // probably a platform upgrade so we need to fix them |
| 1471 | FTS *fts; |
| 1472 | FTSENT *p; |
| 1473 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1474 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1475 | PLOG(ERROR) << "Failed to fts_open " << path; |
| 1476 | return -1; |
| 1477 | } |
| 1478 | while ((p = fts_read(fts)) != NULL) { |
| 1479 | switch (p->fts_info) { |
| 1480 | case FTS_DP: |
| 1481 | if (chmod(p->fts_accpath, target_mode) != 0) { |
| 1482 | PLOG(WARNING) << "Failed to chmod " << p->fts_path; |
| 1483 | } |
| 1484 | // Intentional fall through to also set GID |
| 1485 | case FTS_F: |
| 1486 | if (chown(p->fts_accpath, -1, gid) != 0) { |
| 1487 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 1488 | } |
| 1489 | break; |
| 1490 | case FTS_SL: |
| 1491 | case FTS_SLNONE: |
| 1492 | if (lchown(p->fts_accpath, -1, gid) != 0) { |
| 1493 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 1494 | } |
| 1495 | break; |
| 1496 | } |
| 1497 | } |
| 1498 | fts_close(fts); |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 1502 | } // namespace installd |
| 1503 | } // namespace android |