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> |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 26 | #include <sys/statvfs.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 27 | |
Elliott Hughes | e4ec9eb | 2015-12-04 15:39:32 -0800 | [diff] [blame] | 28 | #include <android-base/logging.h> |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 31 | #include <android-base/unique_fd.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 32 | #include <cutils/fs.h> |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 33 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 34 | #include <log/log.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 35 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 36 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 37 | #include "globals.h" // extern variables. |
| 38 | |
| 39 | #ifndef LOG_TAG |
| 40 | #define LOG_TAG "installd" |
| 41 | #endif |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 42 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 43 | #define DEBUG_XATTRS 0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 44 | |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 45 | using android::base::EndsWith; |
Mathieu Chartier | 89883e3 | 2018-11-01 11:39:00 -0700 | [diff] [blame] | 46 | using android::base::Fdopendir; |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 47 | using android::base::StringPrintf; |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 48 | using android::base::unique_fd; |
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 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Create the path name where package data should be stored for the given |
| 97 | * volume UUID, package name, and user ID. An empty UUID is assumed to be |
| 98 | * internal storage. |
| 99 | * Compared to create_data_user_ce_package_path this method always return the |
| 100 | * ".../user/..." directory. |
| 101 | */ |
| 102 | std::string create_data_user_ce_package_path_as_user_link( |
| 103 | const char* volume_uuid, userid_t userid, const char* package_name) { |
| 104 | check_package_name(package_name); |
| 105 | std::string data(create_data_path(volume_uuid)); |
| 106 | return StringPrintf("%s/user/%u/%s", data.c_str(), userid, package_name); |
| 107 | } |
| 108 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 109 | std::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user, |
| 110 | const char* package_name, ino_t ce_data_inode) { |
| 111 | // For testing purposes, rely on the inode when defined; this could be |
| 112 | // optimized to use access() in the future. |
| 113 | auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name); |
| 114 | if (ce_data_inode != 0) { |
| 115 | auto user_path = create_data_user_ce_path(volume_uuid, user); |
| 116 | DIR* dir = opendir(user_path.c_str()); |
| 117 | if (dir == nullptr) { |
| 118 | PLOG(ERROR) << "Failed to opendir " << user_path; |
| 119 | return fallback; |
| 120 | } |
| 121 | |
| 122 | struct dirent* ent; |
| 123 | while ((ent = readdir(dir))) { |
| 124 | if (ent->d_ino == ce_data_inode) { |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 125 | auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name); |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 126 | #if DEBUG_XATTRS |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 127 | if (resolved != fallback) { |
| 128 | LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode |
| 129 | << " instead of " << fallback; |
| 130 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 131 | #endif |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 132 | closedir(dir); |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 133 | return resolved; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 134 | } |
| 135 | } |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 136 | LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 137 | closedir(dir); |
| 138 | return fallback; |
| 139 | } else { |
| 140 | return fallback; |
| 141 | } |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 142 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 143 | |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 144 | std::string create_data_user_de_package_path(const char* volume_uuid, |
| 145 | userid_t user, const char* package_name) { |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 146 | check_package_name(package_name); |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 147 | return StringPrintf("%s/%s", |
| 148 | create_data_user_de_path(volume_uuid, user).c_str(), package_name); |
| 149 | } |
| 150 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 151 | std::string create_data_path(const char* volume_uuid) { |
| 152 | if (volume_uuid == nullptr) { |
| 153 | return "/data"; |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 154 | } else if (!strcmp(volume_uuid, "TEST")) { |
| 155 | CHECK(property_get_bool("ro.debuggable", false)); |
| 156 | return "/data/local/tmp"; |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 157 | } else { |
| 158 | CHECK(is_valid_filename(volume_uuid)); |
| 159 | return StringPrintf("/mnt/expand/%s", volume_uuid); |
| 160 | } |
| 161 | } |
| 162 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 163 | /** |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 164 | * Create the path name for app data. |
| 165 | */ |
| 166 | std::string create_data_app_path(const char* volume_uuid) { |
| 167 | return StringPrintf("%s/app", create_data_path(volume_uuid).c_str()); |
| 168 | } |
| 169 | |
| 170 | /** |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 171 | * Create the path name for user data for a certain userid. |
cjbao | 75d4e57 | 2017-04-12 00:12:24 +0800 | [diff] [blame] | 172 | * Keep same implementation as vold to minimize path walking overhead |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 173 | */ |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 174 | 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] | 175 | std::string data(create_data_path(volume_uuid)); |
cjbao | 75d4e57 | 2017-04-12 00:12:24 +0800 | [diff] [blame] | 176 | if (volume_uuid == nullptr && userid == 0) { |
| 177 | std::string legacy = StringPrintf("%s/data", data.c_str()); |
| 178 | struct stat sb; |
| 179 | if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 180 | /* /data/data is dir, return /data/data for legacy system */ |
| 181 | return legacy; |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 182 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 183 | } |
cjbao | 75d4e57 | 2017-04-12 00:12:24 +0800 | [diff] [blame] | 184 | return StringPrintf("%s/user/%u", data.c_str(), userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 188 | * Create the path name for device encrypted user data for a certain userid. |
| 189 | */ |
| 190 | std::string create_data_user_de_path(const char* volume_uuid, userid_t userid) { |
| 191 | std::string data(create_data_path(volume_uuid)); |
| 192 | return StringPrintf("%s/user_de/%u", data.c_str(), userid); |
| 193 | } |
| 194 | |
Narayan Kamath | dfdfb79 | 2019-01-14 15:21:52 +0000 | [diff] [blame^] | 195 | |
| 196 | std::string create_data_misc_ce_rollback_path(const char* volume_uuid, userid_t user) { |
| 197 | return StringPrintf("%s/misc_ce/%u/rollback", create_data_path(volume_uuid).c_str(), user); |
| 198 | } |
| 199 | |
| 200 | std::string create_data_misc_de_rollback_path(const char* volume_uuid, userid_t user) { |
| 201 | return StringPrintf("%s/misc_de/%u/rollback", create_data_path(volume_uuid).c_str(), user); |
| 202 | } |
| 203 | |
| 204 | std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid, |
| 205 | userid_t user, const char* package_name) { |
| 206 | return StringPrintf("%s/%s", |
| 207 | create_data_misc_ce_rollback_path(volume_uuid, user).c_str(), package_name); |
| 208 | } |
| 209 | |
| 210 | std::string create_data_misc_de_rollback_package_path(const char* volume_uuid, |
| 211 | userid_t user, const char* package_name) { |
| 212 | return StringPrintf("%s/%s", |
| 213 | create_data_misc_de_rollback_path(volume_uuid, user).c_str(), package_name); |
| 214 | } |
| 215 | |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 216 | /** |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 217 | * Create the path name for media for a certain userid. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 218 | */ |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 219 | std::string create_data_media_path(const char* volume_uuid, userid_t userid) { |
| 220 | 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] | 221 | } |
| 222 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 223 | std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) { |
| 224 | return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name); |
| 225 | } |
| 226 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 227 | std::string create_data_media_package_path(const char* volume_uuid, userid_t userid, |
| 228 | const char* data_type, const char* package_name) { |
| 229 | return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(), |
| 230 | data_type, package_name); |
| 231 | } |
| 232 | |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 233 | std::string create_data_misc_legacy_path(userid_t userid) { |
| 234 | return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid); |
| 235 | } |
| 236 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 237 | std::string create_primary_cur_profile_dir_path(userid_t userid) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 238 | return StringPrintf("%s/cur/%u", android_profiles_dir.c_str(), userid); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 241 | std::string create_primary_current_profile_package_dir_path(userid_t user, |
| 242 | const std::string& package_name) { |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 243 | check_package_name(package_name.c_str()); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 244 | return StringPrintf("%s/%s", |
| 245 | 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] | 246 | } |
| 247 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 248 | std::string create_primary_ref_profile_dir_path() { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 249 | return StringPrintf("%s/ref", android_profiles_dir.c_str()); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 252 | 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] | 253 | check_package_name(package_name.c_str()); |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 254 | return StringPrintf("%s/ref/%s", android_profiles_dir.c_str(), package_name.c_str()); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 257 | std::string create_data_dalvik_cache_path() { |
| 258 | return "/data/dalvik-cache"; |
| 259 | } |
| 260 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 261 | // Keep profile paths in sync with ActivityThread and LoadedApk. |
| 262 | const std::string PROFILE_EXT = ".prof"; |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 263 | const std::string CURRENT_PROFILE_EXT = ".cur"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 264 | const std::string SNAPSHOT_PROFILE_EXT = ".snapshot"; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 265 | |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 266 | // Gets the parent directory and the file name for the given secondary dex path. |
| 267 | // Returns true on success, false on failure (if the dex_path does not have the expected |
| 268 | // structure). |
| 269 | static bool get_secondary_dex_location(const std::string& dex_path, |
| 270 | std::string* out_dir_name, std::string* out_file_name) { |
| 271 | size_t dirIndex = dex_path.rfind('/'); |
| 272 | if (dirIndex == std::string::npos) { |
| 273 | return false; |
| 274 | } |
| 275 | if (dirIndex == dex_path.size() - 1) { |
| 276 | return false; |
| 277 | } |
| 278 | *out_dir_name = dex_path.substr(0, dirIndex); |
| 279 | *out_file_name = dex_path.substr(dirIndex + 1); |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 284 | std::string create_current_profile_path(userid_t user, const std::string& package_name, |
| 285 | const std::string& location, bool is_secondary_dex) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 286 | if (is_secondary_dex) { |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 287 | // Secondary dex current profiles are stored next to the dex files under the oat folder. |
| 288 | std::string dex_dir; |
| 289 | std::string dex_name; |
| 290 | CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name)) |
| 291 | << "Unexpected dir structure for secondary dex " << location; |
| 292 | return StringPrintf("%s/oat/%s%s%s", |
| 293 | dex_dir.c_str(), dex_name.c_str(), CURRENT_PROFILE_EXT.c_str(), |
| 294 | PROFILE_EXT.c_str()); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 295 | } else { |
| 296 | // Profiles for primary apks are under /data/misc/profiles/cur. |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 297 | std::string profile_dir = create_primary_current_profile_package_dir_path( |
| 298 | user, package_name); |
| 299 | return StringPrintf("%s/%s", profile_dir.c_str(), location.c_str()); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 303 | std::string create_reference_profile_path(const std::string& package_name, |
| 304 | const std::string& location, bool is_secondary_dex) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 305 | if (is_secondary_dex) { |
| 306 | // Secondary dex reference profiles are stored next to the dex files under the oat folder. |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 307 | std::string dex_dir; |
| 308 | std::string dex_name; |
| 309 | CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name)) |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 310 | << "Unexpected dir structure for secondary dex " << location; |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 311 | return StringPrintf("%s/oat/%s%s", |
| 312 | dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str()); |
| 313 | } else { |
| 314 | // Reference profiles for primary apks are stored in /data/misc/profile/ref. |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 315 | std::string profile_dir = create_primary_reference_profile_package_dir_path(package_name); |
| 316 | return StringPrintf("%s/%s", profile_dir.c_str(), location.c_str()); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 317 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 320 | std::string create_snapshot_profile_path(const std::string& package, |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 321 | const std::string& profile_name) { |
| 322 | std::string ref_profile = create_reference_profile_path(package, profile_name, |
| 323 | /*is_secondary_dex*/ false); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 324 | return ref_profile + SNAPSHOT_PROFILE_EXT; |
| 325 | } |
| 326 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 327 | std::vector<userid_t> get_known_users(const char* volume_uuid) { |
| 328 | std::vector<userid_t> users; |
| 329 | |
| 330 | // We always have an owner |
| 331 | users.push_back(0); |
| 332 | |
| 333 | std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX); |
| 334 | DIR* dir = opendir(path.c_str()); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 335 | if (dir == nullptr) { |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 336 | // Unable to discover other users, but at least return owner |
| 337 | PLOG(ERROR) << "Failed to opendir " << path; |
| 338 | return users; |
| 339 | } |
| 340 | |
| 341 | struct dirent* ent; |
| 342 | while ((ent = readdir(dir))) { |
| 343 | if (ent->d_type != DT_DIR) { |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | char* end; |
| 348 | userid_t user = strtol(ent->d_name, &end, 10); |
| 349 | if (*end == '\0' && user != 0) { |
| 350 | LOG(DEBUG) << "Found valid user " << user; |
| 351 | users.push_back(user); |
| 352 | } |
| 353 | } |
| 354 | closedir(dir); |
| 355 | |
| 356 | return users; |
| 357 | } |
| 358 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 359 | int calculate_tree_size(const std::string& path, int64_t* size, |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 360 | int32_t include_gid, int32_t exclude_gid, bool exclude_apps) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 361 | FTS *fts; |
| 362 | FTSENT *p; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 363 | int64_t matchedSize = 0; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 364 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 365 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 366 | if (errno != ENOENT) { |
| 367 | PLOG(ERROR) << "Failed to fts_open " << path; |
| 368 | } |
| 369 | return -1; |
| 370 | } |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 371 | while ((p = fts_read(fts)) != nullptr) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 372 | switch (p->fts_info) { |
| 373 | case FTS_D: |
| 374 | case FTS_DEFAULT: |
| 375 | case FTS_F: |
| 376 | case FTS_SL: |
| 377 | case FTS_SLNONE: |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 378 | int32_t uid = p->fts_statp->st_uid; |
| 379 | int32_t gid = p->fts_statp->st_gid; |
| 380 | int32_t user_uid = multiuser_get_app_id(uid); |
| 381 | int32_t user_gid = multiuser_get_app_id(gid); |
| 382 | if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END) |
| 383 | || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END) |
| 384 | || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) { |
| 385 | // Don't traverse inside or measure |
| 386 | fts_set(fts, p, FTS_SKIP); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 387 | break; |
| 388 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 389 | if (include_gid != -1 && gid != include_gid) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 390 | break; |
| 391 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 392 | if (exclude_gid != -1 && gid == exclude_gid) { |
| 393 | break; |
| 394 | } |
| 395 | matchedSize += (p->fts_statp->st_blocks * 512); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 396 | break; |
| 397 | } |
| 398 | } |
| 399 | fts_close(fts); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 400 | #if MEASURE_DEBUG |
| 401 | if ((include_gid == -1) && (exclude_gid == -1)) { |
| 402 | LOG(DEBUG) << "Measured " << path << " size " << matchedSize; |
| 403 | } else { |
| 404 | LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid |
| 405 | << " exclude " << exclude_gid; |
| 406 | } |
| 407 | #endif |
| 408 | *size += matchedSize; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 412 | /** |
| 413 | * Checks whether the package name is valid. Returns -1 on error and |
| 414 | * 0 on success. |
| 415 | */ |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 416 | bool is_valid_package_name(const std::string& packageName) { |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 417 | // This logic is borrowed from PackageParser.java |
| 418 | bool hasSep = false; |
| 419 | bool front = true; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 420 | |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 421 | auto it = packageName.begin(); |
| 422 | for (; it != packageName.end() && *it != '-'; it++) { |
| 423 | char c = *it; |
| 424 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { |
| 425 | front = false; |
| 426 | continue; |
| 427 | } |
| 428 | if (!front) { |
| 429 | if ((c >= '0' && c <= '9') || c == '_') { |
| 430 | continue; |
| 431 | } |
| 432 | } |
| 433 | if (c == '.') { |
| 434 | hasSep = true; |
| 435 | front = true; |
| 436 | continue; |
| 437 | } |
| 438 | LOG(WARNING) << "Bad package character " << c << " in " << packageName; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 439 | return false; |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Jeff Sharkey | ab7ac8d | 2017-03-08 12:39:46 -0700 | [diff] [blame] | 442 | if (front) { |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 443 | LOG(WARNING) << "Missing separator in " << packageName; |
| 444 | return false; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 447 | for (; it != packageName.end(); it++) { |
| 448 | char c = *it; |
| 449 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue; |
| 450 | if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue; |
| 451 | LOG(WARNING) << "Bad suffix character " << c << " in " << packageName; |
| 452 | return false; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 455 | return true; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 458 | static int _delete_dir_contents(DIR *d, |
| 459 | int (*exclusion_predicate)(const char *name, const int is_dir)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 460 | { |
| 461 | int result = 0; |
| 462 | struct dirent *de; |
| 463 | int dfd; |
| 464 | |
| 465 | dfd = dirfd(d); |
| 466 | |
| 467 | if (dfd < 0) return -1; |
| 468 | |
| 469 | while ((de = readdir(d))) { |
| 470 | const char *name = de->d_name; |
| 471 | |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 472 | /* check using the exclusion predicate, if provided */ |
| 473 | if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) { |
| 474 | continue; |
| 475 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 476 | |
| 477 | if (de->d_type == DT_DIR) { |
Chih-Hung Hsieh | 99d9fb1 | 2014-09-11 14:44:46 -0700 | [diff] [blame] | 478 | int subfd; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 479 | DIR *subdir; |
| 480 | |
| 481 | /* always skip "." and ".." */ |
| 482 | if (name[0] == '.') { |
| 483 | if (name[1] == 0) continue; |
| 484 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 485 | } |
| 486 | |
Nick Kralevich | 8b7acac | 2015-08-10 13:43:00 -0700 | [diff] [blame] | 487 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 488 | if (subfd < 0) { |
| 489 | ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); |
| 490 | result = -1; |
| 491 | continue; |
| 492 | } |
| 493 | subdir = fdopendir(subfd); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 494 | if (subdir == nullptr) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 495 | ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); |
| 496 | close(subfd); |
| 497 | result = -1; |
| 498 | continue; |
| 499 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 500 | if (_delete_dir_contents(subdir, exclusion_predicate)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 501 | result = -1; |
| 502 | } |
| 503 | closedir(subdir); |
| 504 | if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { |
| 505 | ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); |
| 506 | result = -1; |
| 507 | } |
| 508 | } else { |
| 509 | if (unlinkat(dfd, name, 0) < 0) { |
| 510 | ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); |
| 511 | result = -1; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return result; |
| 517 | } |
| 518 | |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 519 | int delete_dir_contents(const std::string& pathname, bool ignore_if_missing) { |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 520 | return delete_dir_contents(pathname.c_str(), 0, nullptr, ignore_if_missing); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 523 | int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) { |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 524 | return delete_dir_contents(pathname.c_str(), 1, nullptr, ignore_if_missing); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 527 | int delete_dir_contents(const char *pathname, |
| 528 | int also_delete_dir, |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 529 | int (*exclusion_predicate)(const char*, const int), |
| 530 | bool ignore_if_missing) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 531 | { |
| 532 | int res = 0; |
| 533 | DIR *d; |
| 534 | |
| 535 | d = opendir(pathname); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 536 | if (d == nullptr) { |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 537 | if (ignore_if_missing && (errno == ENOENT)) { |
| 538 | return 0; |
| 539 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 540 | ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno)); |
| 541 | return -errno; |
| 542 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 543 | res = _delete_dir_contents(d, exclusion_predicate); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 544 | closedir(d); |
| 545 | if (also_delete_dir) { |
| 546 | if (rmdir(pathname)) { |
| 547 | ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno)); |
| 548 | res = -1; |
| 549 | } |
| 550 | } |
| 551 | return res; |
| 552 | } |
| 553 | |
| 554 | int delete_dir_contents_fd(int dfd, const char *name) |
| 555 | { |
| 556 | int fd, res; |
| 557 | DIR *d; |
| 558 | |
Nick Kralevich | 8b7acac | 2015-08-10 13:43:00 -0700 | [diff] [blame] | 559 | fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 560 | if (fd < 0) { |
| 561 | ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); |
| 562 | return -1; |
| 563 | } |
| 564 | d = fdopendir(fd); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 565 | if (d == nullptr) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 566 | ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); |
| 567 | close(fd); |
| 568 | return -1; |
| 569 | } |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 570 | res = _delete_dir_contents(d, nullptr); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 571 | closedir(d); |
| 572 | return res; |
| 573 | } |
| 574 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 575 | static int _copy_owner_permissions(int srcfd, int dstfd) |
| 576 | { |
| 577 | struct stat st; |
| 578 | if (fstat(srcfd, &st) != 0) { |
| 579 | return -1; |
| 580 | } |
| 581 | if (fchmod(dstfd, st.st_mode) != 0) { |
| 582 | return -1; |
| 583 | } |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group) |
| 588 | { |
| 589 | int result = 0; |
| 590 | if (_copy_owner_permissions(sdfd, ddfd) != 0) { |
| 591 | ALOGE("_copy_dir_files failed to copy dir permissions\n"); |
| 592 | } |
| 593 | if (fchown(ddfd, owner, group) != 0) { |
| 594 | ALOGE("_copy_dir_files failed to change dir owner\n"); |
| 595 | } |
| 596 | |
| 597 | DIR *ds = fdopendir(sdfd); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 598 | if (ds == nullptr) { |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 599 | ALOGE("Couldn't fdopendir: %s\n", strerror(errno)); |
| 600 | return -1; |
| 601 | } |
| 602 | struct dirent *de; |
| 603 | while ((de = readdir(ds))) { |
| 604 | if (de->d_type != DT_REG) { |
| 605 | continue; |
| 606 | } |
| 607 | |
| 608 | const char *name = de->d_name; |
| 609 | int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); |
| 610 | int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600); |
| 611 | if (fsfd == -1 || fdfd == -1) { |
| 612 | ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); |
| 613 | } else { |
| 614 | if (_copy_owner_permissions(fsfd, fdfd) != 0) { |
| 615 | ALOGE("Failed to change file permissions\n"); |
| 616 | } |
| 617 | if (fchown(fdfd, owner, group) != 0) { |
| 618 | ALOGE("Failed to change file owner\n"); |
| 619 | } |
| 620 | |
| 621 | char buf[8192]; |
| 622 | ssize_t size; |
| 623 | while ((size = read(fsfd, buf, sizeof(buf))) > 0) { |
| 624 | write(fdfd, buf, size); |
| 625 | } |
| 626 | if (size < 0) { |
| 627 | ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); |
| 628 | result = -1; |
| 629 | } |
| 630 | } |
| 631 | close(fdfd); |
| 632 | close(fsfd); |
| 633 | } |
| 634 | |
| 635 | return result; |
| 636 | } |
| 637 | |
| 638 | int copy_dir_files(const char *srcname, |
| 639 | const char *dstname, |
| 640 | uid_t owner, |
| 641 | uid_t group) |
| 642 | { |
| 643 | int res = 0; |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 644 | DIR *ds = nullptr; |
| 645 | DIR *dd = nullptr; |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 646 | |
| 647 | ds = opendir(srcname); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 648 | if (ds == nullptr) { |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 649 | ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno)); |
| 650 | return -errno; |
| 651 | } |
| 652 | |
| 653 | mkdir(dstname, 0600); |
| 654 | dd = opendir(dstname); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 655 | if (dd == nullptr) { |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 656 | ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno)); |
| 657 | closedir(ds); |
| 658 | return -errno; |
| 659 | } |
| 660 | |
| 661 | int sdfd = dirfd(ds); |
| 662 | int ddfd = dirfd(dd); |
| 663 | if (sdfd != -1 && ddfd != -1) { |
| 664 | res = _copy_dir_files(sdfd, ddfd, owner, group); |
| 665 | } else { |
| 666 | res = -errno; |
| 667 | } |
| 668 | closedir(dd); |
| 669 | closedir(ds); |
| 670 | return res; |
| 671 | } |
| 672 | |
Jeff Sharkey | a836c47 | 2017-04-02 23:29:30 -0600 | [diff] [blame] | 673 | int64_t data_disk_free(const std::string& data_path) { |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 674 | struct statvfs sfs; |
| 675 | if (statvfs(data_path.c_str(), &sfs) == 0) { |
Jeff Sharkey | 4f7be17 | 2017-08-11 15:13:31 -0600 | [diff] [blame] | 676 | return static_cast<int64_t>(sfs.f_bavail) * sfs.f_frsize; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 677 | } else { |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 678 | PLOG(ERROR) << "Couldn't statvfs " << data_path; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 679 | return -1; |
| 680 | } |
| 681 | } |
| 682 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 683 | int get_path_inode(const std::string& path, ino_t *inode) { |
| 684 | struct stat buf; |
| 685 | memset(&buf, 0, sizeof(buf)); |
| 686 | if (stat(path.c_str(), &buf) != 0) { |
| 687 | PLOG(WARNING) << "Failed to stat " << path; |
| 688 | return -1; |
| 689 | } else { |
| 690 | *inode = buf.st_ino; |
| 691 | return 0; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Write the inode of a specific child file into the given xattr on the |
| 697 | * parent directory. This allows you to find the child later, even if its |
| 698 | * name is encrypted. |
| 699 | */ |
| 700 | int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { |
| 701 | ino_t inode = 0; |
| 702 | uint64_t inode_raw = 0; |
| 703 | auto path = StringPrintf("%s/%s", parent.c_str(), name); |
| 704 | |
| 705 | if (get_path_inode(path, &inode) != 0) { |
| 706 | // Path probably doesn't exist yet; ignore |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | // Check to see if already set correctly |
| 711 | if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { |
| 712 | if (inode_raw == inode) { |
| 713 | // Already set correctly; skip writing |
| 714 | return 0; |
| 715 | } else { |
| 716 | PLOG(WARNING) << "Mismatched inode value; found " << inode |
| 717 | << " on disk but marked value was " << inode_raw << "; overwriting"; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | inode_raw = inode; |
Jeff Sharkey | 4ed6507 | 2016-07-22 11:38:54 -0600 | [diff] [blame] | 722 | 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] | 723 | PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent; |
| 724 | return -1; |
| 725 | } else { |
| 726 | return 0; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Read the inode of a specific child file from the given xattr on the |
| 732 | * parent directory. Returns a currently valid path for that child, which |
| 733 | * might have an encrypted name. |
| 734 | */ |
| 735 | std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { |
| 736 | ino_t inode = 0; |
| 737 | uint64_t inode_raw = 0; |
| 738 | auto fallback = StringPrintf("%s/%s", parent.c_str(), name); |
| 739 | |
| 740 | // Lookup the inode value written earlier |
| 741 | if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { |
| 742 | inode = inode_raw; |
| 743 | } |
| 744 | |
| 745 | // For testing purposes, rely on the inode when defined; this could be |
| 746 | // optimized to use access() in the future. |
| 747 | if (inode != 0) { |
| 748 | DIR* dir = opendir(parent.c_str()); |
| 749 | if (dir == nullptr) { |
| 750 | PLOG(ERROR) << "Failed to opendir " << parent; |
| 751 | return fallback; |
| 752 | } |
| 753 | |
| 754 | struct dirent* ent; |
| 755 | while ((ent = readdir(dir))) { |
| 756 | if (ent->d_ino == inode) { |
| 757 | auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name); |
| 758 | #if DEBUG_XATTRS |
| 759 | if (resolved != fallback) { |
| 760 | LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode |
| 761 | << " instead of " << fallback; |
| 762 | } |
| 763 | #endif |
| 764 | closedir(dir); |
| 765 | return resolved; |
| 766 | } |
| 767 | } |
| 768 | LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback; |
| 769 | closedir(dir); |
| 770 | return fallback; |
| 771 | } else { |
| 772 | return fallback; |
| 773 | } |
| 774 | } |
| 775 | |
Ryuki Nakamura | c7342f8 | 2017-09-30 11:57:00 +0900 | [diff] [blame] | 776 | void remove_path_xattr(const std::string& path, const char* inode_xattr) { |
| 777 | if (removexattr(path.c_str(), inode_xattr) && errno != ENODATA) { |
| 778 | PLOG(ERROR) << "Failed to remove xattr " << inode_xattr << " at " << path; |
| 779 | } |
| 780 | } |
| 781 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 782 | /** |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 783 | * Validate that the path is valid in the context of the provided directory. |
| 784 | * The path is allowed to have at most one subdirectory and no indirections |
| 785 | * to top level directories (i.e. have ".."). |
| 786 | */ |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 787 | static int validate_path(const std::string& dir, const std::string& path, int maxSubdirs) { |
| 788 | // Argument sanity checking |
| 789 | if (dir.find('/') != 0 || dir.rfind('/') != dir.size() - 1 |
| 790 | || dir.find("..") != std::string::npos) { |
| 791 | LOG(ERROR) << "Invalid directory " << dir; |
| 792 | return -1; |
| 793 | } |
| 794 | if (path.find("..") != std::string::npos) { |
| 795 | LOG(ERROR) << "Invalid path " << path; |
| 796 | return -1; |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 799 | if (path.compare(0, dir.size(), dir) != 0) { |
| 800 | // Common case, path isn't under directory |
| 801 | return -1; |
| 802 | } |
| 803 | |
| 804 | // Count number of subdirectories |
| 805 | auto pos = path.find('/', dir.size()); |
| 806 | int count = 0; |
| 807 | while (pos != std::string::npos) { |
Jeff Sharkey | 172fac0 | 2017-10-06 13:09:46 -0600 | [diff] [blame] | 808 | auto next = path.find('/', pos + 1); |
| 809 | if (next > pos + 1) { |
| 810 | count++; |
| 811 | } |
| 812 | pos = next; |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | if (count > maxSubdirs) { |
| 816 | LOG(ERROR) << "Invalid path depth " << path << " when tested against " << dir; |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 817 | return -1; |
| 818 | } |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | /** |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 824 | * Checks whether a path points to a system app (.apk file). Returns 0 |
| 825 | * if it is a system app or -1 if it is not. |
| 826 | */ |
| 827 | int validate_system_app_path(const char* path) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 828 | std::string path_ = path; |
| 829 | for (const auto& dir : android_system_dirs) { |
| 830 | if (validate_path(dir, path, 1) == 0) { |
| 831 | return 0; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 832 | } |
| 833 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 834 | return -1; |
| 835 | } |
| 836 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 837 | bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 838 | const char* volume_uuid, int uid, int storage_flag) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 839 | CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE); |
| 840 | |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 841 | // Empty paths are not allowed. |
| 842 | if (dex_path.empty()) { return false; } |
| 843 | // First character should always be '/'. No relative paths. |
| 844 | if (dex_path[0] != '/') { return false; } |
| 845 | // The last character should not be '/'. |
| 846 | if (dex_path[dex_path.size() - 1] == '/') { return false; } |
| 847 | // There should be no '.' after the directory marker. |
| 848 | if (dex_path.find("/.") != std::string::npos) { return false; } |
| 849 | // The path should be at most PKG_PATH_MAX long. |
| 850 | if (dex_path.size() > PKG_PATH_MAX) { return false; } |
| 851 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 852 | // The dex_path should be under the app data directory. |
| 853 | std::string app_private_dir = storage_flag == FLAG_STORAGE_CE |
Calin Juravle | dd42e27 | 2017-09-11 11:50:36 -0700 | [diff] [blame] | 854 | ? create_data_user_ce_package_path( |
| 855 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()) |
| 856 | : create_data_user_de_package_path( |
| 857 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()); |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 858 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 859 | if (strncmp(dex_path.c_str(), app_private_dir.c_str(), app_private_dir.size()) != 0) { |
| 860 | // The check above might fail if the dex file is accessed via the /data/user/0 symlink. |
| 861 | // If that's the case, attempt to validate against the user data link. |
| 862 | std::string app_private_dir_symlink = create_data_user_ce_package_path_as_user_link( |
| 863 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()); |
| 864 | if (strncmp(dex_path.c_str(), app_private_dir_symlink.c_str(), |
| 865 | app_private_dir_symlink.size()) != 0) { |
Calin Juravle | dd42e27 | 2017-09-11 11:50:36 -0700 | [diff] [blame] | 866 | return false; |
| 867 | } |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 868 | } |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 869 | |
| 870 | // If we got here we have a valid path. |
| 871 | return true; |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 874 | /** |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 875 | * Check whether path points to a valid path for an APK file. The path must |
| 876 | * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within |
| 877 | * that path. Returns -1 when an invalid path is encountered and 0 when a valid path |
| 878 | * is encountered. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 879 | */ |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 880 | static int validate_apk_path_internal(const std::string& path, int maxSubdirs) { |
| 881 | if (validate_path(android_app_dir, path, maxSubdirs) == 0) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 882 | return 0; |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 883 | } else if (validate_path(android_app_private_dir, path, maxSubdirs) == 0) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 884 | return 0; |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 885 | } else if (validate_path(android_app_ephemeral_dir, path, maxSubdirs) == 0) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 886 | return 0; |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 887 | } else if (validate_path(android_asec_dir, path, maxSubdirs) == 0) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 888 | return 0; |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 889 | } else if (android::base::StartsWith(path, android_mnt_expand_dir)) { |
| 890 | // Rewrite the path as if it were on internal storage, and test that |
| 891 | size_t end = path.find('/', android_mnt_expand_dir.size() + 1); |
| 892 | if (end != std::string::npos) { |
| 893 | auto modified = path; |
| 894 | modified.replace(0, end + 1, android_data_dir); |
| 895 | return validate_apk_path_internal(modified, maxSubdirs); |
| 896 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 897 | } |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 898 | return -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 901 | int validate_apk_path(const char* path) { |
| 902 | return validate_apk_path_internal(path, 1 /* maxSubdirs */); |
| 903 | } |
| 904 | |
| 905 | int validate_apk_path_subdirs(const char* path) { |
| 906 | return validate_apk_path_internal(path, 3 /* maxSubdirs */); |
| 907 | } |
| 908 | |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 909 | int ensure_config_user_dirs(userid_t userid) { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 910 | // writable by system, readable by any app within the same user |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 911 | const int uid = multiuser_get_uid(userid, AID_SYSTEM); |
| 912 | const int gid = multiuser_get_uid(userid, AID_EVERYBODY); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 913 | |
| 914 | // Ensure /data/misc/user/<userid> exists |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 915 | auto path = create_data_misc_legacy_path(userid); |
| 916 | return fs_prepare_dir(path.c_str(), 0750, uid, gid); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 917 | } |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 918 | |
| 919 | int wait_child(pid_t pid) |
| 920 | { |
| 921 | int status; |
| 922 | pid_t got_pid; |
| 923 | |
| 924 | while (1) { |
| 925 | got_pid = waitpid(pid, &status, 0); |
| 926 | if (got_pid == -1 && errno == EINTR) { |
| 927 | printf("waitpid interrupted, retrying\n"); |
| 928 | } else { |
| 929 | break; |
| 930 | } |
| 931 | } |
| 932 | if (got_pid != pid) { |
| 933 | ALOGW("waitpid failed: wanted %d, got %d: %s\n", |
| 934 | (int) pid, (int) got_pid, strerror(errno)); |
| 935 | return 1; |
| 936 | } |
| 937 | |
| 938 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 939 | return 0; |
| 940 | } else { |
| 941 | return status; /* always nonzero */ |
| 942 | } |
| 943 | } |
| 944 | |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 945 | /** |
| 946 | * Prepare an app cache directory, which offers to fix-up the GID and |
| 947 | * directory mode flags during a platform upgrade. |
| 948 | * The app cache directory path will be 'parent'/'name'. |
| 949 | */ |
| 950 | int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, |
| 951 | uid_t uid, gid_t gid) { |
| 952 | auto path = StringPrintf("%s/%s", parent.c_str(), name); |
| 953 | struct stat st; |
| 954 | if (stat(path.c_str(), &st) != 0) { |
| 955 | if (errno == ENOENT) { |
| 956 | // This is fine, just create it |
| 957 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { |
| 958 | PLOG(ERROR) << "Failed to prepare " << path; |
| 959 | return -1; |
| 960 | } else { |
| 961 | return 0; |
| 962 | } |
| 963 | } else { |
| 964 | PLOG(ERROR) << "Failed to stat " << path; |
| 965 | return -1; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); |
| 970 | if (st.st_uid != uid) { |
| 971 | // Mismatched UID is real trouble; we can't recover |
| 972 | LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid |
| 973 | << " but expected " << uid; |
| 974 | return -1; |
| 975 | } else if (st.st_gid == gid && actual_mode == target_mode) { |
| 976 | // Everything looks good! |
| 977 | return 0; |
Jeff Sharkey | e59c85c | 2017-04-02 21:53:14 -0600 | [diff] [blame] | 978 | } else { |
| 979 | // Mismatched GID/mode is recoverable; fall through to update |
| 980 | LOG(DEBUG) << "Mismatched cache GID/mode at " << path << ": found " << st.st_gid |
Shubham Ajmera | ec0afbf | 2017-09-14 11:07:33 -0700 | [diff] [blame] | 981 | << "/" << actual_mode << " but expected " << gid << "/" << target_mode; |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | // Directory is owned correctly, but GID or mode mismatch means it's |
| 985 | // probably a platform upgrade so we need to fix them |
| 986 | FTS *fts; |
| 987 | FTSENT *p; |
| 988 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 989 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 990 | PLOG(ERROR) << "Failed to fts_open " << path; |
| 991 | return -1; |
| 992 | } |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 993 | while ((p = fts_read(fts)) != nullptr) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 994 | switch (p->fts_info) { |
| 995 | case FTS_DP: |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 996 | if (chmod(p->fts_path, target_mode) != 0) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 997 | PLOG(WARNING) << "Failed to chmod " << p->fts_path; |
| 998 | } |
Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 999 | [[fallthrough]]; // to also set GID |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1000 | case FTS_F: |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1001 | if (chown(p->fts_path, -1, gid) != 0) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1002 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 1003 | } |
| 1004 | break; |
| 1005 | case FTS_SL: |
| 1006 | case FTS_SLNONE: |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1007 | if (lchown(p->fts_path, -1, gid) != 0) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1008 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 1009 | } |
| 1010 | break; |
| 1011 | } |
| 1012 | } |
| 1013 | fts_close(fts); |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1017 | // Collect all non empty profiles from the given directory and puts then into profile_paths. |
| 1018 | // The profiles are identified based on PROFILE_EXT extension. |
| 1019 | // If a subdirectory or profile file cannot be opened the method logs a warning and moves on. |
| 1020 | // It returns true if there were no errors at all, and false otherwise. |
| 1021 | static bool collect_profiles(DIR* d, |
| 1022 | const std::string& current_path, |
| 1023 | std::vector<std::string>* profiles_paths) { |
| 1024 | int32_t dir_fd = dirfd(d); |
| 1025 | if (dir_fd < 0) { |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | bool result = true; |
| 1030 | struct dirent* dir_entry; |
| 1031 | while ((dir_entry = readdir(d))) { |
| 1032 | std::string name = dir_entry->d_name; |
| 1033 | std::string local_path = current_path + "/" + name; |
| 1034 | |
| 1035 | if (dir_entry->d_type == DT_REG) { |
| 1036 | // Check if this is a non empty profile file. |
| 1037 | if (EndsWith(name, PROFILE_EXT)) { |
| 1038 | struct stat st; |
| 1039 | if (stat(local_path.c_str(), &st) != 0) { |
| 1040 | PLOG(WARNING) << "Cannot stat local path " << local_path; |
| 1041 | result = false; |
| 1042 | continue; |
| 1043 | } else if (st.st_size > 0) { |
| 1044 | profiles_paths->push_back(local_path); |
| 1045 | } |
| 1046 | } |
| 1047 | } else if (dir_entry->d_type == DT_DIR) { |
| 1048 | // always skip "." and ".." |
| 1049 | if (name == "." || name == "..") { |
| 1050 | continue; |
| 1051 | } |
| 1052 | |
| 1053 | unique_fd subdir_fd(openat(dir_fd, name.c_str(), |
| 1054 | O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)); |
| 1055 | if (subdir_fd < 0) { |
| 1056 | PLOG(WARNING) << "Could not open dir path " << local_path; |
| 1057 | result = false; |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
Mathieu Chartier | 89883e3 | 2018-11-01 11:39:00 -0700 | [diff] [blame] | 1061 | DIR* subdir = Fdopendir(std::move(subdir_fd)); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1062 | if (subdir == nullptr) { |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1063 | PLOG(WARNING) << "Could not open dir path " << local_path; |
| 1064 | result = false; |
| 1065 | continue; |
| 1066 | } |
| 1067 | bool new_result = collect_profiles(subdir, local_path, profiles_paths); |
| 1068 | result = result && new_result; |
| 1069 | if (closedir(subdir) != 0) { |
| 1070 | PLOG(WARNING) << "Could not close dir path " << local_path; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | return result; |
| 1076 | } |
| 1077 | |
| 1078 | bool collect_profiles(std::vector<std::string>* profiles_paths) { |
| 1079 | DIR* d = opendir(android_profiles_dir.c_str()); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1080 | if (d == nullptr) { |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1081 | return false; |
| 1082 | } else { |
| 1083 | return collect_profiles(d, android_profiles_dir, profiles_paths); |
| 1084 | } |
| 1085 | } |
| 1086 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 1087 | } // namespace installd |
| 1088 | } // namespace android |