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