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