Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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 |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "Utils.h" |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 18 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 19 | #include "Process.h" |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 20 | #include "sehandle.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 21 | |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 22 | #include <android-base/chrono_utils.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 23 | #include <android-base/file.h> |
| 24 | #include <android-base/logging.h> |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 25 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 27 | #include <android-base/strings.h> |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 28 | #include <android-base/unique_fd.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 29 | #include <cutils/fs.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 30 | #include <logwrap/logwrap.h> |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 31 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 32 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 33 | #include <dirent.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 34 | #include <fcntl.h> |
| 35 | #include <linux/fs.h> |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 36 | #include <linux/posix_acl.h> |
| 37 | #include <linux/posix_acl_xattr.h> |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 38 | #include <mntent.h> |
| 39 | #include <stdio.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 40 | #include <stdlib.h> |
| 41 | #include <sys/mount.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 42 | #include <sys/stat.h> |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 43 | #include <sys/statvfs.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 44 | #include <sys/sysmacros.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <sys/wait.h> |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 47 | #include <sys/xattr.h> |
Paul Crowley | 747b421 | 2019-04-05 04:09:57 -0700 | [diff] [blame] | 48 | #include <unistd.h> |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 49 | |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 50 | #include <list> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 51 | #include <mutex> |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 52 | #include <regex> |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 53 | #include <thread> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 54 | |
| 55 | #ifndef UMOUNT_NOFOLLOW |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 56 | #define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */ |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 57 | #endif |
| 58 | |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 59 | using namespace std::chrono_literals; |
Martijn Coenen | ba9868b | 2020-01-31 15:49:24 +0100 | [diff] [blame] | 60 | using android::base::EndsWith; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 61 | using android::base::ReadFileToString; |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 62 | using android::base::StartsWith; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 63 | using android::base::StringPrintf; |
| 64 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 65 | namespace android { |
| 66 | namespace vold { |
| 67 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 68 | security_context_t sBlkidContext = nullptr; |
| 69 | security_context_t sBlkidUntrustedContext = nullptr; |
| 70 | security_context_t sFsckContext = nullptr; |
| 71 | security_context_t sFsckUntrustedContext = nullptr; |
| 72 | |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 73 | bool sSleepOnUnmount = true; |
| 74 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 75 | static const char* kBlkidPath = "/system/bin/blkid"; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 76 | static const char* kKeyPath = "/data/misc/vold"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 77 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 78 | static const char* kProcFilesystems = "/proc/filesystems"; |
| 79 | |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 80 | static const char* kAndroidDir = "/Android/"; |
| 81 | static const char* kAppDataDir = "/Android/data/"; |
| 82 | static const char* kAppMediaDir = "/Android/media/"; |
| 83 | static const char* kAppObbDir = "/Android/obb/"; |
| 84 | |
Jeff Sharkey | ae4f85d | 2017-10-18 17:02:21 -0600 | [diff] [blame] | 85 | // Lock used to protect process-level SELinux changes from racing with each |
| 86 | // other between multiple threads. |
| 87 | static std::mutex kSecurityLock; |
| 88 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 89 | status_t CreateDeviceNode(const std::string& path, dev_t dev) { |
Jeff Sharkey | ae4f85d | 2017-10-18 17:02:21 -0600 | [diff] [blame] | 90 | std::lock_guard<std::mutex> lock(kSecurityLock); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 91 | const char* cpath = path.c_str(); |
| 92 | status_t res = 0; |
| 93 | |
| 94 | char* secontext = nullptr; |
| 95 | if (sehandle) { |
| 96 | if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) { |
| 97 | setfscreatecon(secontext); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | mode_t mode = 0660 | S_IFBLK; |
| 102 | if (mknod(cpath, mode, dev) < 0) { |
| 103 | if (errno != EEXIST) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 104 | PLOG(ERROR) << "Failed to create device node for " << major(dev) << ":" << minor(dev) |
| 105 | << " at " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 106 | res = -errno; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (secontext) { |
| 111 | setfscreatecon(nullptr); |
| 112 | freecon(secontext); |
| 113 | } |
| 114 | |
| 115 | return res; |
| 116 | } |
| 117 | |
| 118 | status_t DestroyDeviceNode(const std::string& path) { |
| 119 | const char* cpath = path.c_str(); |
| 120 | if (TEMP_FAILURE_RETRY(unlink(cpath))) { |
| 121 | return -errno; |
| 122 | } else { |
| 123 | return OK; |
| 124 | } |
| 125 | } |
| 126 | |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 127 | // Sets a default ACL where the owner and group can read/write/execute. |
| 128 | // Other users aren't allowed anything. |
| 129 | int SetDefault770Acl(const std::string& path, uid_t uid, gid_t gid) { |
| 130 | if (IsFilesystemSupported("sdcardfs")) { |
| 131 | // sdcardfs magically takes care of this |
| 132 | return OK; |
| 133 | } |
| 134 | |
| 135 | static constexpr size_t size = |
| 136 | sizeof(posix_acl_xattr_header) + 3 * sizeof(posix_acl_xattr_entry); |
| 137 | auto buf = std::make_unique<uint8_t[]>(size); |
| 138 | |
| 139 | posix_acl_xattr_header* acl_header = reinterpret_cast<posix_acl_xattr_header*>(buf.get()); |
| 140 | acl_header->a_version = POSIX_ACL_XATTR_VERSION; |
| 141 | |
| 142 | posix_acl_xattr_entry* entry = |
| 143 | reinterpret_cast<posix_acl_xattr_entry*>(buf.get() + sizeof(posix_acl_xattr_header)); |
| 144 | |
| 145 | entry[0].e_tag = ACL_USER_OBJ; |
| 146 | entry[0].e_perm = ACL_READ | ACL_WRITE | ACL_EXECUTE; |
| 147 | entry[0].e_id = uid; |
| 148 | |
| 149 | entry[1].e_tag = ACL_GROUP_OBJ; |
| 150 | entry[1].e_perm = ACL_READ | ACL_WRITE | ACL_EXECUTE; |
| 151 | entry[1].e_id = gid; |
| 152 | |
| 153 | entry[2].e_tag = ACL_OTHER; |
| 154 | entry[2].e_perm = 0; |
| 155 | entry[2].e_id = 0; |
| 156 | |
| 157 | int ret = setxattr(path.c_str(), XATTR_NAME_POSIX_ACL_DEFAULT, acl_header, size, 0); |
| 158 | |
| 159 | if (ret != 0) { |
| 160 | PLOG(ERROR) << "Failed to set default ACL on " << path; |
| 161 | } |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
Martijn Coenen | 5fe1b16 | 2020-02-06 18:57:47 +0100 | [diff] [blame] | 166 | int SetQuotaInherit(const std::string& path) { |
| 167 | unsigned long flags; |
| 168 | |
| 169 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC))); |
| 170 | if (fd == -1) { |
| 171 | PLOG(ERROR) << "Failed to open " << path << " to set project id inheritance."; |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | int ret = ioctl(fd, FS_IOC_GETFLAGS, &flags); |
| 176 | if (ret == -1) { |
| 177 | PLOG(ERROR) << "Failed to get flags for " << path << " to set project id inheritance."; |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | flags |= FS_PROJINHERIT_FL; |
| 182 | |
| 183 | ret = ioctl(fd, FS_IOC_SETFLAGS, &flags); |
| 184 | if (ret == -1) { |
| 185 | PLOG(ERROR) << "Failed to set flags for " << path << " to set project id inheritance."; |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | int SetQuotaProjectId(const std::string& path, long projectId) { |
Martijn Coenen | fb42bc4 | 2020-01-16 01:25:27 +0100 | [diff] [blame] | 193 | struct fsxattr fsx; |
| 194 | |
| 195 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC))); |
| 196 | if (fd == -1) { |
| 197 | PLOG(ERROR) << "Failed to open " << path << " to set project id."; |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | int ret = ioctl(fd, FS_IOC_FSGETXATTR, &fsx); |
| 202 | if (ret == -1) { |
| 203 | PLOG(ERROR) << "Failed to get extended attributes for " << path << " to get project id."; |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | fsx.fsx_projid = projectId; |
| 208 | return ioctl(fd, FS_IOC_FSSETXATTR, &fsx); |
| 209 | } |
| 210 | |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 211 | int PrepareDirWithProjectId(const std::string& path, mode_t mode, uid_t uid, gid_t gid, |
| 212 | long projectId) { |
| 213 | int ret = fs_prepare_dir(path.c_str(), mode, uid, gid); |
| 214 | |
| 215 | if (ret != 0) { |
| 216 | return ret; |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 217 | } |
Martijn Coenen | ba9868b | 2020-01-31 15:49:24 +0100 | [diff] [blame] | 218 | |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 219 | if (!IsFilesystemSupported("sdcardfs")) { |
| 220 | ret = SetQuotaProjectId(path, projectId); |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 226 | int PrepareAppDirFromRoot(const std::string& path, const std::string& root, int appUid) { |
| 227 | long projectId; |
| 228 | size_t pos; |
| 229 | int ret = 0; |
| 230 | |
| 231 | // Make sure the Android/ directories exist and are setup correctly |
| 232 | ret = PrepareAndroidDirs(root); |
| 233 | if (ret != 0) { |
| 234 | LOG(ERROR) << "Failed to prepare Android/ directories."; |
| 235 | return ret; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 238 | // Now create the application-specific subdir(s) |
| 239 | // path is something like /data/media/0/Android/data/com.foo/files |
| 240 | // First, chop off the volume root, eg /data/media/0 |
| 241 | std::string pathFromRoot = path.substr(root.length()); |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 242 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 243 | uid_t uid = appUid; |
| 244 | gid_t gid = AID_MEDIA_RW; |
| 245 | std::string appDir; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 246 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 247 | // Check that the next part matches one of the allowed Android/ dirs |
| 248 | if (StartsWith(pathFromRoot, kAppDataDir)) { |
| 249 | appDir = kAppDataDir; |
| 250 | if (!IsFilesystemSupported("sdcardfs")) { |
| 251 | gid = AID_EXT_DATA_RW; |
| 252 | } |
| 253 | } else if (StartsWith(pathFromRoot, kAppMediaDir)) { |
| 254 | appDir = kAppMediaDir; |
| 255 | if (!IsFilesystemSupported("sdcardfs")) { |
| 256 | gid = AID_MEDIA_RW; |
| 257 | } |
Ricky Wai | e50ddb7 | 2020-02-17 18:57:01 +0000 | [diff] [blame^] | 258 | } else if (StartsWith(pathFromRoot, kAppObbDir)) { |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 259 | appDir = kAppObbDir; |
| 260 | if (!IsFilesystemSupported("sdcardfs")) { |
| 261 | gid = AID_EXT_OBB_RW; |
| 262 | } |
| 263 | } else { |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 264 | LOG(ERROR) << "Invalid application directory: " << path; |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 268 | // mode = 770, plus sticky bit on directory to inherit GID when apps |
| 269 | // create subdirs |
| 270 | mode_t mode = S_IRWXU | S_IRWXG | S_ISGID; |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 271 | // the project ID for application-specific directories is directly |
| 272 | // derived from their uid |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 273 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 274 | // Chop off the generic application-specific part, eg /Android/data/ |
| 275 | // this leaves us with something like com.foo/files/ |
| 276 | std::string leftToCreate = pathFromRoot.substr(appDir.length()); |
| 277 | if (!EndsWith(leftToCreate, "/")) { |
| 278 | leftToCreate += "/"; |
| 279 | } |
| 280 | std::string pathToCreate = root + appDir; |
| 281 | int depth = 0; |
| 282 | bool withinCache = false; |
| 283 | while ((pos = leftToCreate.find('/')) != std::string::npos) { |
| 284 | std::string component = leftToCreate.substr(0, pos + 1); |
| 285 | leftToCreate = leftToCreate.erase(0, pos + 1); |
| 286 | pathToCreate = pathToCreate + component; |
| 287 | |
| 288 | if (appDir == kAppDataDir && depth == 1 && component == "cache/") { |
| 289 | // All dirs use the "app" project ID, except for the cache dirs in |
| 290 | // Android/data, eg Android/data/com.foo/cache |
| 291 | // Note that this "sticks" - eg subdirs of this dir need the same |
| 292 | // project ID. |
| 293 | withinCache = true; |
| 294 | } |
| 295 | if (withinCache) { |
| 296 | projectId = uid - AID_APP_START + AID_CACHE_GID_START; |
| 297 | } else { |
| 298 | projectId = uid - AID_APP_START + AID_EXT_GID_START; |
| 299 | } |
| 300 | ret = PrepareDirWithProjectId(pathToCreate, mode, uid, gid, projectId); |
| 301 | if (ret != 0) { |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | if (depth == 0) { |
| 306 | // Set the default ACL on the top-level application-specific directories, |
| 307 | // to ensure that even if applications run with a umask of 0077, |
| 308 | // new directories within these directories will allow the GID |
| 309 | // specified here to write; this is necessary for apps like |
| 310 | // installers and MTP, that require access here. |
| 311 | // |
| 312 | // See man (5) acl for more details. |
| 313 | ret = SetDefault770Acl(pathToCreate, uid, gid); |
| 314 | if (ret != 0) { |
| 315 | return ret; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | depth++; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 322 | return OK; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 325 | status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid) { |
Jeff Sharkey | ae4f85d | 2017-10-18 17:02:21 -0600 | [diff] [blame] | 326 | std::lock_guard<std::mutex> lock(kSecurityLock); |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 327 | const char* cpath = path.c_str(); |
| 328 | |
| 329 | char* secontext = nullptr; |
| 330 | if (sehandle) { |
| 331 | if (!selabel_lookup(sehandle, &secontext, cpath, S_IFDIR)) { |
| 332 | setfscreatecon(secontext); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | int res = fs_prepare_dir(cpath, mode, uid, gid); |
| 337 | |
| 338 | if (secontext) { |
| 339 | setfscreatecon(nullptr); |
| 340 | freecon(secontext); |
| 341 | } |
| 342 | |
| 343 | if (res == 0) { |
| 344 | return OK; |
| 345 | } else { |
| 346 | return -errno; |
| 347 | } |
| 348 | } |
| 349 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 350 | status_t ForceUnmount(const std::string& path) { |
| 351 | const char* cpath = path.c_str(); |
| 352 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 353 | return OK; |
| 354 | } |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 355 | // Apps might still be handling eject request, so wait before |
| 356 | // we start sending signals |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 357 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 358 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 359 | KillProcessesWithOpenFiles(path, SIGINT); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 360 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 361 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 362 | return OK; |
| 363 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 364 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 365 | KillProcessesWithOpenFiles(path, SIGTERM); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 366 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 367 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 368 | return OK; |
| 369 | } |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 370 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 371 | KillProcessesWithOpenFiles(path, SIGKILL); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 372 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 373 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 374 | return OK; |
| 375 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 376 | PLOG(INFO) << "ForceUnmount failed"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 377 | return -errno; |
| 378 | } |
| 379 | |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 380 | status_t KillProcessesUsingPath(const std::string& path) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 381 | if (KillProcessesWithOpenFiles(path, SIGINT) == 0) { |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 382 | return OK; |
| 383 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 384 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 385 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 386 | if (KillProcessesWithOpenFiles(path, SIGTERM) == 0) { |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 387 | return OK; |
| 388 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 389 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 390 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 391 | if (KillProcessesWithOpenFiles(path, SIGKILL) == 0) { |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 392 | return OK; |
| 393 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 394 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 395 | |
| 396 | // Send SIGKILL a second time to determine if we've |
| 397 | // actually killed everyone with open files |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 398 | if (KillProcessesWithOpenFiles(path, SIGKILL) == 0) { |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 399 | return OK; |
| 400 | } |
| 401 | PLOG(ERROR) << "Failed to kill processes using " << path; |
| 402 | return -EBUSY; |
| 403 | } |
| 404 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 405 | status_t BindMount(const std::string& source, const std::string& target) { |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 406 | if (UnmountTree(target) < 0) { |
| 407 | return -errno; |
| 408 | } |
| 409 | if (TEMP_FAILURE_RETRY(mount(source.c_str(), target.c_str(), nullptr, MS_BIND, nullptr)) < 0) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 410 | PLOG(ERROR) << "Failed to bind mount " << source << " to " << target; |
| 411 | return -errno; |
| 412 | } |
| 413 | return OK; |
| 414 | } |
| 415 | |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 416 | status_t Symlink(const std::string& target, const std::string& linkpath) { |
| 417 | if (Unlink(linkpath) < 0) { |
| 418 | return -errno; |
| 419 | } |
| 420 | if (TEMP_FAILURE_RETRY(symlink(target.c_str(), linkpath.c_str())) < 0) { |
| 421 | PLOG(ERROR) << "Failed to create symlink " << linkpath << " to " << target; |
| 422 | return -errno; |
| 423 | } |
| 424 | return OK; |
| 425 | } |
| 426 | |
| 427 | status_t Unlink(const std::string& linkpath) { |
| 428 | if (TEMP_FAILURE_RETRY(unlink(linkpath.c_str())) < 0 && errno != EINVAL && errno != ENOENT) { |
| 429 | PLOG(ERROR) << "Failed to unlink " << linkpath; |
| 430 | return -errno; |
| 431 | } |
| 432 | return OK; |
| 433 | } |
| 434 | |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 435 | status_t CreateDir(const std::string& dir, mode_t mode) { |
| 436 | struct stat sb; |
| 437 | if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &sb)) == 0) { |
| 438 | if (S_ISDIR(sb.st_mode)) { |
| 439 | return OK; |
| 440 | } else if (TEMP_FAILURE_RETRY(unlink(dir.c_str())) == -1) { |
| 441 | PLOG(ERROR) << "Failed to unlink " << dir; |
| 442 | return -errno; |
| 443 | } |
| 444 | } else if (errno != ENOENT) { |
| 445 | PLOG(ERROR) << "Failed to stat " << dir; |
| 446 | return -errno; |
| 447 | } |
Sudheer Shanka | 6d285ce | 2019-02-19 14:12:20 -0800 | [diff] [blame] | 448 | if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), mode)) == -1 && errno != EEXIST) { |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 449 | PLOG(ERROR) << "Failed to mkdir " << dir; |
| 450 | return -errno; |
| 451 | } |
| 452 | return OK; |
| 453 | } |
| 454 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 455 | bool FindValue(const std::string& raw, const std::string& key, std::string* value) { |
| 456 | auto qual = key + "=\""; |
Paul Crowley | 95abfa0 | 2019-02-05 15:33:34 -0800 | [diff] [blame] | 457 | size_t start = 0; |
| 458 | while (true) { |
| 459 | start = raw.find(qual, start); |
| 460 | if (start == std::string::npos) return false; |
| 461 | if (start == 0 || raw[start - 1] == ' ') { |
| 462 | break; |
| 463 | } |
| 464 | start += 1; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 465 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 466 | start += qual.length(); |
| 467 | |
| 468 | auto end = raw.find("\"", start); |
| 469 | if (end == std::string::npos) return false; |
| 470 | |
| 471 | *value = raw.substr(start, end - start); |
| 472 | return true; |
| 473 | } |
| 474 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 475 | static status_t readMetadata(const std::string& path, std::string* fsType, std::string* fsUuid, |
| 476 | std::string* fsLabel, bool untrusted) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 477 | fsType->clear(); |
| 478 | fsUuid->clear(); |
| 479 | fsLabel->clear(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 480 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 481 | std::vector<std::string> cmd; |
| 482 | cmd.push_back(kBlkidPath); |
| 483 | cmd.push_back("-c"); |
| 484 | cmd.push_back("/dev/null"); |
Jeff Sharkey | eddf9bd | 2015-08-12 16:04:35 -0700 | [diff] [blame] | 485 | cmd.push_back("-s"); |
| 486 | cmd.push_back("TYPE"); |
| 487 | cmd.push_back("-s"); |
| 488 | cmd.push_back("UUID"); |
| 489 | cmd.push_back("-s"); |
| 490 | cmd.push_back("LABEL"); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 491 | cmd.push_back(path); |
| 492 | |
| 493 | std::vector<std::string> output; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 494 | status_t res = ForkExecvp(cmd, &output, untrusted ? sBlkidUntrustedContext : sBlkidContext); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 495 | if (res != OK) { |
| 496 | LOG(WARNING) << "blkid failed to identify " << path; |
| 497 | return res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 500 | for (const auto& line : output) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 501 | // Extract values from blkid output, if defined |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 502 | FindValue(line, "TYPE", fsType); |
| 503 | FindValue(line, "UUID", fsUuid); |
| 504 | FindValue(line, "LABEL", fsLabel); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 507 | return OK; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 510 | status_t ReadMetadata(const std::string& path, std::string* fsType, std::string* fsUuid, |
| 511 | std::string* fsLabel) { |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 512 | return readMetadata(path, fsType, fsUuid, fsLabel, false); |
| 513 | } |
| 514 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 515 | status_t ReadMetadataUntrusted(const std::string& path, std::string* fsType, std::string* fsUuid, |
| 516 | std::string* fsLabel) { |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 517 | return readMetadata(path, fsType, fsUuid, fsLabel, true); |
| 518 | } |
| 519 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 520 | static std::vector<const char*> ConvertToArgv(const std::vector<std::string>& args) { |
| 521 | std::vector<const char*> argv; |
| 522 | argv.reserve(args.size() + 1); |
| 523 | for (const auto& arg : args) { |
| 524 | if (argv.empty()) { |
| 525 | LOG(DEBUG) << arg; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 526 | } else { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 527 | LOG(DEBUG) << " " << arg; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 528 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 529 | argv.emplace_back(arg.data()); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 530 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 531 | argv.emplace_back(nullptr); |
| 532 | return argv; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 535 | static status_t ReadLinesFromFdAndLog(std::vector<std::string>* output, |
| 536 | android::base::unique_fd ufd) { |
| 537 | std::unique_ptr<FILE, int (*)(FILE*)> fp(android::base::Fdopen(std::move(ufd), "r"), fclose); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 538 | if (!fp) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 539 | PLOG(ERROR) << "fdopen in ReadLinesFromFdAndLog"; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 540 | return -errno; |
| 541 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 542 | if (output) output->clear(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 543 | char line[1024]; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 544 | while (fgets(line, sizeof(line), fp.get()) != nullptr) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 545 | LOG(DEBUG) << line; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 546 | if (output) output->emplace_back(line); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 547 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 548 | return OK; |
| 549 | } |
| 550 | |
| 551 | status_t ForkExecvp(const std::vector<std::string>& args, std::vector<std::string>* output, |
| 552 | security_context_t context) { |
| 553 | auto argv = ConvertToArgv(args); |
| 554 | |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 555 | android::base::unique_fd pipe_read, pipe_write; |
| 556 | if (!android::base::Pipe(&pipe_read, &pipe_write)) { |
| 557 | PLOG(ERROR) << "Pipe in ForkExecvp"; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 558 | return -errno; |
| 559 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 560 | |
| 561 | pid_t pid = fork(); |
| 562 | if (pid == 0) { |
| 563 | if (context) { |
| 564 | if (setexeccon(context)) { |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 565 | LOG(ERROR) << "Failed to setexeccon in ForkExecvp"; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 566 | abort(); |
| 567 | } |
| 568 | } |
| 569 | pipe_read.reset(); |
Paul Crowley | be857bf | 2018-12-07 12:23:25 -0800 | [diff] [blame] | 570 | if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) { |
| 571 | PLOG(ERROR) << "dup2 in ForkExecvp"; |
| 572 | _exit(EXIT_FAILURE); |
| 573 | } |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 574 | pipe_write.reset(); |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 575 | execvp(argv[0], const_cast<char**>(argv.data())); |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 576 | PLOG(ERROR) << "exec in ForkExecvp"; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 577 | _exit(EXIT_FAILURE); |
| 578 | } |
| 579 | if (pid == -1) { |
| 580 | PLOG(ERROR) << "fork in ForkExecvp"; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 581 | return -errno; |
| 582 | } |
| 583 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 584 | pipe_write.reset(); |
| 585 | auto st = ReadLinesFromFdAndLog(output, std::move(pipe_read)); |
| 586 | if (st != 0) return st; |
| 587 | |
| 588 | int status; |
| 589 | if (waitpid(pid, &status, 0) == -1) { |
| 590 | PLOG(ERROR) << "waitpid in ForkExecvp"; |
| 591 | return -errno; |
| 592 | } |
| 593 | if (!WIFEXITED(status)) { |
| 594 | LOG(ERROR) << "Process did not exit normally, status: " << status; |
| 595 | return -ECHILD; |
| 596 | } |
| 597 | if (WEXITSTATUS(status)) { |
| 598 | LOG(ERROR) << "Process exited with code: " << WEXITSTATUS(status); |
| 599 | return WEXITSTATUS(status); |
| 600 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 601 | return OK; |
| 602 | } |
| 603 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 604 | pid_t ForkExecvpAsync(const std::vector<std::string>& args) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 605 | auto argv = ConvertToArgv(args); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 606 | |
| 607 | pid_t pid = fork(); |
| 608 | if (pid == 0) { |
| 609 | close(STDIN_FILENO); |
| 610 | close(STDOUT_FILENO); |
| 611 | close(STDERR_FILENO); |
| 612 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 613 | execvp(argv[0], const_cast<char**>(argv.data())); |
| 614 | PLOG(ERROR) << "exec in ForkExecvpAsync"; |
| 615 | _exit(EXIT_FAILURE); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 616 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 617 | if (pid == -1) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 618 | PLOG(ERROR) << "fork in ForkExecvpAsync"; |
| 619 | return -1; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 620 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 621 | return pid; |
| 622 | } |
| 623 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 624 | status_t ReadRandomBytes(size_t bytes, std::string& out) { |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 625 | out.resize(bytes); |
| 626 | return ReadRandomBytes(bytes, &out[0]); |
| 627 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 628 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 629 | status_t ReadRandomBytes(size_t bytes, char* buf) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 630 | int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 631 | if (fd == -1) { |
| 632 | return -errno; |
| 633 | } |
| 634 | |
Eric Biggers | 0ef7bfd | 2019-01-16 13:05:34 -0800 | [diff] [blame] | 635 | ssize_t n; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 636 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], bytes))) > 0) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 637 | bytes -= n; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 638 | buf += n; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 639 | } |
Elliott Hughes | a623108 | 2015-05-15 18:34:24 -0700 | [diff] [blame] | 640 | close(fd); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 641 | |
| 642 | if (bytes == 0) { |
| 643 | return OK; |
| 644 | } else { |
| 645 | return -EIO; |
| 646 | } |
| 647 | } |
| 648 | |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 649 | status_t GenerateRandomUuid(std::string& out) { |
| 650 | status_t res = ReadRandomBytes(16, out); |
| 651 | if (res == OK) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 652 | out[6] &= 0x0f; /* clear version */ |
| 653 | out[6] |= 0x40; /* set to version 4 */ |
| 654 | out[8] &= 0x3f; /* clear variant */ |
| 655 | out[8] |= 0x80; /* set to IETF variant */ |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 656 | } |
| 657 | return res; |
| 658 | } |
| 659 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 660 | status_t HexToStr(const std::string& hex, std::string& str) { |
| 661 | str.clear(); |
| 662 | bool even = true; |
| 663 | char cur = 0; |
| 664 | for (size_t i = 0; i < hex.size(); i++) { |
| 665 | int val = 0; |
| 666 | switch (hex[i]) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 667 | // clang-format off |
| 668 | case ' ': case '-': case ':': continue; |
| 669 | case 'f': case 'F': val = 15; break; |
| 670 | case 'e': case 'E': val = 14; break; |
| 671 | case 'd': case 'D': val = 13; break; |
| 672 | case 'c': case 'C': val = 12; break; |
| 673 | case 'b': case 'B': val = 11; break; |
| 674 | case 'a': case 'A': val = 10; break; |
| 675 | case '9': val = 9; break; |
| 676 | case '8': val = 8; break; |
| 677 | case '7': val = 7; break; |
| 678 | case '6': val = 6; break; |
| 679 | case '5': val = 5; break; |
| 680 | case '4': val = 4; break; |
| 681 | case '3': val = 3; break; |
| 682 | case '2': val = 2; break; |
| 683 | case '1': val = 1; break; |
| 684 | case '0': val = 0; break; |
| 685 | default: return -EINVAL; |
| 686 | // clang-format on |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (even) { |
| 690 | cur = val << 4; |
| 691 | } else { |
| 692 | cur += val; |
| 693 | str.push_back(cur); |
| 694 | cur = 0; |
| 695 | } |
| 696 | even = !even; |
| 697 | } |
| 698 | return even ? OK : -EINVAL; |
| 699 | } |
| 700 | |
| 701 | static const char* kLookup = "0123456789abcdef"; |
| 702 | |
| 703 | status_t StrToHex(const std::string& str, std::string& hex) { |
| 704 | hex.clear(); |
| 705 | for (size_t i = 0; i < str.size(); i++) { |
Jeff Sharkey | ef36975 | 2015-04-29 15:57:48 -0700 | [diff] [blame] | 706 | hex.push_back(kLookup[(str[i] & 0xF0) >> 4]); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 707 | hex.push_back(kLookup[str[i] & 0x0F]); |
| 708 | } |
| 709 | return OK; |
| 710 | } |
| 711 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 712 | status_t StrToHex(const KeyBuffer& str, KeyBuffer& hex) { |
| 713 | hex.clear(); |
| 714 | for (size_t i = 0; i < str.size(); i++) { |
| 715 | hex.push_back(kLookup[(str.data()[i] & 0xF0) >> 4]); |
| 716 | hex.push_back(kLookup[str.data()[i] & 0x0F]); |
| 717 | } |
| 718 | return OK; |
| 719 | } |
| 720 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 721 | status_t NormalizeHex(const std::string& in, std::string& out) { |
| 722 | std::string tmp; |
| 723 | if (HexToStr(in, tmp)) { |
| 724 | return -EINVAL; |
| 725 | } |
| 726 | return StrToHex(tmp, out); |
| 727 | } |
| 728 | |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 729 | status_t GetBlockDevSize(int fd, uint64_t* size) { |
| 730 | if (ioctl(fd, BLKGETSIZE64, size)) { |
| 731 | return -errno; |
| 732 | } |
| 733 | |
| 734 | return OK; |
| 735 | } |
| 736 | |
| 737 | status_t GetBlockDevSize(const std::string& path, uint64_t* size) { |
| 738 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 739 | status_t res = OK; |
| 740 | |
| 741 | if (fd < 0) { |
| 742 | return -errno; |
| 743 | } |
| 744 | |
| 745 | res = GetBlockDevSize(fd, size); |
| 746 | |
| 747 | close(fd); |
| 748 | |
| 749 | return res; |
| 750 | } |
| 751 | |
| 752 | status_t GetBlockDev512Sectors(const std::string& path, uint64_t* nr_sec) { |
| 753 | uint64_t size; |
| 754 | status_t res = GetBlockDevSize(path, &size); |
| 755 | |
| 756 | if (res != OK) { |
| 757 | return res; |
| 758 | } |
| 759 | |
| 760 | *nr_sec = size / 512; |
| 761 | |
| 762 | return OK; |
| 763 | } |
| 764 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 765 | uint64_t GetFreeBytes(const std::string& path) { |
| 766 | struct statvfs sb; |
| 767 | if (statvfs(path.c_str(), &sb) == 0) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 768 | return (uint64_t)sb.f_bavail * sb.f_frsize; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 769 | } else { |
| 770 | return -1; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // TODO: borrowed from frameworks/native/libs/diskusage/ which should |
| 775 | // eventually be migrated into system/ |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 776 | static int64_t stat_size(struct stat* s) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 777 | int64_t blksize = s->st_blksize; |
| 778 | // count actual blocks used instead of nominal file size |
| 779 | int64_t size = s->st_blocks * 512; |
| 780 | |
| 781 | if (blksize) { |
| 782 | /* round up to filesystem block size */ |
| 783 | size = (size + blksize - 1) & (~(blksize - 1)); |
| 784 | } |
| 785 | |
| 786 | return size; |
| 787 | } |
| 788 | |
| 789 | // TODO: borrowed from frameworks/native/libs/diskusage/ which should |
| 790 | // eventually be migrated into system/ |
| 791 | int64_t calculate_dir_size(int dfd) { |
| 792 | int64_t size = 0; |
| 793 | struct stat s; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 794 | DIR* d; |
| 795 | struct dirent* de; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 796 | |
| 797 | d = fdopendir(dfd); |
| 798 | if (d == NULL) { |
| 799 | close(dfd); |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | while ((de = readdir(d))) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 804 | const char* name = de->d_name; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 805 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 806 | size += stat_size(&s); |
| 807 | } |
| 808 | if (de->d_type == DT_DIR) { |
| 809 | int subfd; |
| 810 | |
| 811 | /* always skip "." and ".." */ |
| 812 | if (name[0] == '.') { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 813 | if (name[1] == 0) continue; |
| 814 | if ((name[1] == '.') && (name[2] == 0)) continue; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 817 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 818 | if (subfd >= 0) { |
| 819 | size += calculate_dir_size(subfd); |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | closedir(d); |
| 824 | return size; |
| 825 | } |
| 826 | |
| 827 | uint64_t GetTreeBytes(const std::string& path) { |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 828 | int dirfd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 829 | if (dirfd < 0) { |
| 830 | PLOG(WARNING) << "Failed to open " << path; |
| 831 | return -1; |
| 832 | } else { |
Josh Gao | 72fb1a6 | 2018-05-29 19:05:16 -0700 | [diff] [blame] | 833 | return calculate_dir_size(dirfd); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 837 | bool IsFilesystemSupported(const std::string& fsType) { |
| 838 | std::string supported; |
| 839 | if (!ReadFileToString(kProcFilesystems, &supported)) { |
| 840 | PLOG(ERROR) << "Failed to read supported filesystems"; |
| 841 | return false; |
| 842 | } |
| 843 | return supported.find(fsType + "\n") != std::string::npos; |
| 844 | } |
| 845 | |
| 846 | status_t WipeBlockDevice(const std::string& path) { |
| 847 | status_t res = -1; |
| 848 | const char* c_path = path.c_str(); |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 849 | uint64_t range[2] = {0, 0}; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 850 | |
| 851 | int fd = TEMP_FAILURE_RETRY(open(c_path, O_RDWR | O_CLOEXEC)); |
| 852 | if (fd == -1) { |
| 853 | PLOG(ERROR) << "Failed to open " << path; |
| 854 | goto done; |
| 855 | } |
| 856 | |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 857 | if (GetBlockDevSize(fd, &range[1]) != OK) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 858 | PLOG(ERROR) << "Failed to determine size of " << path; |
| 859 | goto done; |
| 860 | } |
| 861 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 862 | LOG(INFO) << "About to discard " << range[1] << " on " << path; |
| 863 | if (ioctl(fd, BLKDISCARD, &range) == 0) { |
| 864 | LOG(INFO) << "Discard success on " << path; |
| 865 | res = 0; |
| 866 | } else { |
| 867 | PLOG(ERROR) << "Discard failure on " << path; |
| 868 | } |
| 869 | |
| 870 | done: |
| 871 | close(fd); |
| 872 | return res; |
| 873 | } |
| 874 | |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 875 | static bool isValidFilename(const std::string& name) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 876 | if (name.empty() || (name == ".") || (name == "..") || (name.find('/') != std::string::npos)) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 877 | return false; |
| 878 | } else { |
| 879 | return true; |
| 880 | } |
| 881 | } |
| 882 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 883 | std::string BuildKeyPath(const std::string& partGuid) { |
| 884 | return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str()); |
| 885 | } |
| 886 | |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 887 | std::string BuildDataSystemLegacyPath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 888 | return StringPrintf("%s/system/users/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 889 | } |
| 890 | |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 891 | std::string BuildDataSystemCePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 892 | return StringPrintf("%s/system_ce/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | std::string BuildDataSystemDePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 896 | return StringPrintf("%s/system_de/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 899 | std::string BuildDataMiscLegacyPath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 900 | return StringPrintf("%s/misc/user/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 901 | } |
| 902 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 903 | std::string BuildDataMiscCePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 904 | return StringPrintf("%s/misc_ce/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | std::string BuildDataMiscDePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 908 | return StringPrintf("%s/misc_de/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Calin Juravle | 79f55a4 | 2016-02-17 20:14:46 +0000 | [diff] [blame] | 911 | // Keep in sync with installd (frameworks/native/cmds/installd/utils.h) |
| 912 | std::string BuildDataProfilesDePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 913 | return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath("").c_str(), userId); |
Calin Juravle | 79f55a4 | 2016-02-17 20:14:46 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 916 | std::string BuildDataVendorCePath(userid_t userId) { |
| 917 | return StringPrintf("%s/vendor_ce/%u", BuildDataPath("").c_str(), userId); |
| 918 | } |
| 919 | |
| 920 | std::string BuildDataVendorDePath(userid_t userId) { |
| 921 | return StringPrintf("%s/vendor_de/%u", BuildDataPath("").c_str(), userId); |
| 922 | } |
| 923 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 924 | std::string BuildDataPath(const std::string& volumeUuid) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 925 | // TODO: unify with installd path generation logic |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 926 | if (volumeUuid.empty()) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 927 | return "/data"; |
| 928 | } else { |
| 929 | CHECK(isValidFilename(volumeUuid)); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 930 | return StringPrintf("/mnt/expand/%s", volumeUuid.c_str()); |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 934 | std::string BuildDataMediaCePath(const std::string& volumeUuid, userid_t userId) { |
Jeff Sharkey | fc505c3 | 2015-12-07 17:27:01 -0700 | [diff] [blame] | 935 | // TODO: unify with installd path generation logic |
| 936 | std::string data(BuildDataPath(volumeUuid)); |
| 937 | return StringPrintf("%s/media/%u", data.c_str(), userId); |
| 938 | } |
| 939 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 940 | std::string BuildDataUserCePath(const std::string& volumeUuid, userid_t userId) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 941 | // TODO: unify with installd path generation logic |
| 942 | std::string data(BuildDataPath(volumeUuid)); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 943 | if (volumeUuid.empty() && userId == 0) { |
cjbao | eb50114 | 2017-04-12 00:09:00 +0800 | [diff] [blame] | 944 | std::string legacy = StringPrintf("%s/data", data.c_str()); |
| 945 | struct stat sb; |
| 946 | if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 947 | /* /data/data is dir, return /data/data for legacy system */ |
| 948 | return legacy; |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 949 | } |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 950 | } |
cjbao | eb50114 | 2017-04-12 00:09:00 +0800 | [diff] [blame] | 951 | return StringPrintf("%s/user/%u", data.c_str(), userId); |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 954 | std::string BuildDataUserDePath(const std::string& volumeUuid, userid_t userId) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 955 | // TODO: unify with installd path generation logic |
| 956 | std::string data(BuildDataPath(volumeUuid)); |
| 957 | return StringPrintf("%s/user_de/%u", data.c_str(), userId); |
| 958 | } |
| 959 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 960 | dev_t GetDevice(const std::string& path) { |
| 961 | struct stat sb; |
| 962 | if (stat(path.c_str(), &sb)) { |
| 963 | PLOG(WARNING) << "Failed to stat " << path; |
| 964 | return 0; |
| 965 | } else { |
| 966 | return sb.st_dev; |
| 967 | } |
| 968 | } |
| 969 | |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 970 | status_t RestoreconRecursive(const std::string& path) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 971 | LOG(DEBUG) << "Starting restorecon of " << path; |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 972 | |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 973 | static constexpr const char* kRestoreconString = "selinux.restorecon_recursive"; |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 974 | |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 975 | android::base::SetProperty(kRestoreconString, ""); |
| 976 | android::base::SetProperty(kRestoreconString, path); |
| 977 | |
| 978 | android::base::WaitForProperty(kRestoreconString, path); |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 979 | |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 980 | LOG(DEBUG) << "Finished restorecon of " << path; |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 981 | return OK; |
| 982 | } |
| 983 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 984 | bool Readlinkat(int dirfd, const std::string& path, std::string* result) { |
| 985 | // Shamelessly borrowed from android::base::Readlink() |
| 986 | result->clear(); |
| 987 | |
| 988 | // Most Linux file systems (ext2 and ext4, say) limit symbolic links to |
| 989 | // 4095 bytes. Since we'll copy out into the string anyway, it doesn't |
| 990 | // waste memory to just start there. We add 1 so that we can recognize |
| 991 | // whether it actually fit (rather than being truncated to 4095). |
| 992 | std::vector<char> buf(4095 + 1); |
| 993 | while (true) { |
| 994 | ssize_t size = readlinkat(dirfd, path.c_str(), &buf[0], buf.size()); |
| 995 | // Unrecoverable error? |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 996 | if (size == -1) return false; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 997 | // It fit! (If size == buf.size(), it may have been truncated.) |
| 998 | if (static_cast<size_t>(size) < buf.size()) { |
| 999 | result->assign(&buf[0], size); |
| 1000 | return true; |
| 1001 | } |
| 1002 | // Double our buffer and try again. |
| 1003 | buf.resize(buf.size() * 2); |
Daichi Hirono | 10d3488 | 2016-01-29 14:33:51 +0900 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 1007 | bool IsRunningInEmulator() { |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 1008 | return android::base::GetBoolProperty("ro.kernel.qemu", false); |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Sudheer Shanka | 295fb24 | 2019-01-16 23:04:07 -0800 | [diff] [blame] | 1011 | static status_t findMountPointsWithPrefix(const std::string& prefix, |
| 1012 | std::list<std::string>& mountPoints) { |
| 1013 | // Add a trailing slash if the client didn't provide one so that we don't match /foo/barbaz |
| 1014 | // when the prefix is /foo/bar |
| 1015 | std::string prefixWithSlash(prefix); |
| 1016 | if (prefix.back() != '/') { |
| 1017 | android::base::StringAppendF(&prefixWithSlash, "/"); |
| 1018 | } |
| 1019 | |
| 1020 | std::unique_ptr<FILE, int (*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent); |
| 1021 | if (!mnts) { |
| 1022 | PLOG(ERROR) << "Unable to open /proc/mounts"; |
| 1023 | return -errno; |
| 1024 | } |
| 1025 | |
| 1026 | // Some volumes can be stacked on each other, so force unmount in |
| 1027 | // reverse order to give us the best chance of success. |
| 1028 | struct mntent* mnt; // getmntent returns a thread local, so it's safe. |
| 1029 | while ((mnt = getmntent(mnts.get())) != nullptr) { |
| 1030 | auto mountPoint = std::string(mnt->mnt_dir) + "/"; |
| 1031 | if (android::base::StartsWith(mountPoint, prefixWithSlash)) { |
| 1032 | mountPoints.push_front(mountPoint); |
| 1033 | } |
| 1034 | } |
| 1035 | return OK; |
| 1036 | } |
| 1037 | |
| 1038 | // Unmount all mountpoints that start with prefix. prefix itself doesn't need to be a mountpoint. |
| 1039 | status_t UnmountTreeWithPrefix(const std::string& prefix) { |
| 1040 | std::list<std::string> toUnmount; |
| 1041 | status_t result = findMountPointsWithPrefix(prefix, toUnmount); |
| 1042 | if (result < 0) { |
| 1043 | return result; |
| 1044 | } |
| 1045 | for (const auto& path : toUnmount) { |
| 1046 | if (umount2(path.c_str(), MNT_DETACH)) { |
| 1047 | PLOG(ERROR) << "Failed to unmount " << path; |
| 1048 | result = -errno; |
| 1049 | } |
| 1050 | } |
| 1051 | return result; |
| 1052 | } |
| 1053 | |
| 1054 | status_t UnmountTree(const std::string& mountPoint) { |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 1055 | if (TEMP_FAILURE_RETRY(umount2(mountPoint.c_str(), MNT_DETACH)) < 0 && errno != EINVAL && |
| 1056 | errno != ENOENT) { |
Sudheer Shanka | 295fb24 | 2019-01-16 23:04:07 -0800 | [diff] [blame] | 1057 | PLOG(ERROR) << "Failed to unmount " << mountPoint; |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 1058 | return -errno; |
| 1059 | } |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 1060 | return OK; |
| 1061 | } |
| 1062 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1063 | static status_t delete_dir_contents(DIR* dir) { |
| 1064 | // Shamelessly borrowed from android::installd |
| 1065 | int dfd = dirfd(dir); |
| 1066 | if (dfd < 0) { |
| 1067 | return -errno; |
| 1068 | } |
| 1069 | |
Sudheer Shanka | 6bf1480 | 2019-01-17 13:38:10 -0800 | [diff] [blame] | 1070 | status_t result = OK; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1071 | struct dirent* de; |
| 1072 | while ((de = readdir(dir))) { |
| 1073 | const char* name = de->d_name; |
| 1074 | if (de->d_type == DT_DIR) { |
| 1075 | /* always skip "." and ".." */ |
| 1076 | if (name[0] == '.') { |
| 1077 | if (name[1] == 0) continue; |
| 1078 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 1079 | } |
| 1080 | |
| 1081 | android::base::unique_fd subfd( |
| 1082 | openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)); |
| 1083 | if (subfd.get() == -1) { |
| 1084 | PLOG(ERROR) << "Couldn't openat " << name; |
| 1085 | result = -errno; |
| 1086 | continue; |
| 1087 | } |
Josh Gao | e3c32e0 | 2018-11-05 13:47:28 -0800 | [diff] [blame] | 1088 | std::unique_ptr<DIR, decltype(&closedir)> subdirp( |
| 1089 | android::base::Fdopendir(std::move(subfd)), closedir); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1090 | if (!subdirp) { |
| 1091 | PLOG(ERROR) << "Couldn't fdopendir " << name; |
| 1092 | result = -errno; |
| 1093 | continue; |
| 1094 | } |
| 1095 | result = delete_dir_contents(subdirp.get()); |
| 1096 | if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { |
| 1097 | PLOG(ERROR) << "Couldn't unlinkat " << name; |
| 1098 | result = -errno; |
| 1099 | } |
| 1100 | } else { |
| 1101 | if (unlinkat(dfd, name, 0) < 0) { |
| 1102 | PLOG(ERROR) << "Couldn't unlinkat " << name; |
| 1103 | result = -errno; |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | return result; |
| 1108 | } |
| 1109 | |
| 1110 | status_t DeleteDirContentsAndDir(const std::string& pathname) { |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 1111 | status_t res = DeleteDirContents(pathname); |
| 1112 | if (res < 0) { |
| 1113 | return res; |
| 1114 | } |
Sudheer Shanka | 8255a2b | 2019-02-25 12:21:23 -0800 | [diff] [blame] | 1115 | if (TEMP_FAILURE_RETRY(rmdir(pathname.c_str())) < 0 && errno != ENOENT) { |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 1116 | PLOG(ERROR) << "rmdir failed on " << pathname; |
| 1117 | return -errno; |
| 1118 | } |
| 1119 | LOG(VERBOSE) << "Success: rmdir on " << pathname; |
| 1120 | return OK; |
| 1121 | } |
| 1122 | |
| 1123 | status_t DeleteDirContents(const std::string& pathname) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1124 | // Shamelessly borrowed from android::installd |
| 1125 | std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir(pathname.c_str()), closedir); |
| 1126 | if (!dirp) { |
| 1127 | if (errno == ENOENT) { |
| 1128 | return OK; |
| 1129 | } |
| 1130 | PLOG(ERROR) << "Failed to opendir " << pathname; |
| 1131 | return -errno; |
| 1132 | } |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 1133 | return delete_dir_contents(dirp.get()); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 1136 | // TODO(118708649): fix duplication with init/util.h |
| 1137 | status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout) { |
| 1138 | android::base::Timer t; |
| 1139 | while (t.duration() < timeout) { |
| 1140 | struct stat sb; |
| 1141 | if (stat(filename, &sb) != -1) { |
| 1142 | LOG(INFO) << "wait for '" << filename << "' took " << t; |
| 1143 | return 0; |
| 1144 | } |
| 1145 | std::this_thread::sleep_for(10ms); |
| 1146 | } |
| 1147 | LOG(WARNING) << "wait for '" << filename << "' timed out and took " << t; |
| 1148 | return -1; |
| 1149 | } |
| 1150 | |
Paul Crowley | 621d9b9 | 2018-12-07 15:36:09 -0800 | [diff] [blame] | 1151 | bool FsyncDirectory(const std::string& dirname) { |
| 1152 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dirname.c_str(), O_RDONLY | O_CLOEXEC))); |
| 1153 | if (fd == -1) { |
| 1154 | PLOG(ERROR) << "Failed to open " << dirname; |
| 1155 | return false; |
| 1156 | } |
| 1157 | if (fsync(fd) == -1) { |
| 1158 | if (errno == EROFS || errno == EINVAL) { |
| 1159 | PLOG(WARNING) << "Skip fsync " << dirname |
| 1160 | << " on a file system does not support synchronization"; |
| 1161 | } else { |
| 1162 | PLOG(ERROR) << "Failed to fsync " << dirname; |
| 1163 | return false; |
| 1164 | } |
| 1165 | } |
| 1166 | return true; |
| 1167 | } |
| 1168 | |
Tommy Chiu | 0bd2d11 | 2019-03-26 17:18:09 +0800 | [diff] [blame] | 1169 | bool writeStringToFile(const std::string& payload, const std::string& filename) { |
| 1170 | android::base::unique_fd fd(TEMP_FAILURE_RETRY( |
| 1171 | open(filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0666))); |
| 1172 | if (fd == -1) { |
| 1173 | PLOG(ERROR) << "Failed to open " << filename; |
| 1174 | return false; |
| 1175 | } |
| 1176 | if (!android::base::WriteStringToFd(payload, fd)) { |
| 1177 | PLOG(ERROR) << "Failed to write to " << filename; |
| 1178 | unlink(filename.c_str()); |
| 1179 | return false; |
| 1180 | } |
| 1181 | // fsync as close won't guarantee flush data |
| 1182 | // see close(2), fsync(2) and b/68901441 |
| 1183 | if (fsync(fd) == -1) { |
| 1184 | if (errno == EROFS || errno == EINVAL) { |
| 1185 | PLOG(WARNING) << "Skip fsync " << filename |
| 1186 | << " on a file system does not support synchronization"; |
| 1187 | } else { |
| 1188 | PLOG(ERROR) << "Failed to fsync " << filename; |
| 1189 | unlink(filename.c_str()); |
| 1190 | return false; |
| 1191 | } |
| 1192 | } |
| 1193 | return true; |
| 1194 | } |
| 1195 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1196 | status_t MountUserFuse(userid_t user_id, const std::string& absolute_lower_path, |
| 1197 | const std::string& relative_upper_path, android::base::unique_fd* fuse_fd) { |
| 1198 | std::string pre_fuse_path(StringPrintf("/mnt/user/%d", user_id)); |
| 1199 | std::string fuse_path( |
| 1200 | StringPrintf("%s/%s", pre_fuse_path.c_str(), relative_upper_path.c_str())); |
| 1201 | |
| 1202 | std::string pre_pass_through_path(StringPrintf("/mnt/pass_through/%d", user_id)); |
| 1203 | std::string pass_through_path( |
| 1204 | StringPrintf("%s/%s", pre_pass_through_path.c_str(), relative_upper_path.c_str())); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1205 | |
Zim | 1242be8 | 2020-01-22 18:22:29 +0000 | [diff] [blame] | 1206 | // Ensure that /mnt/user is 0700. With FUSE, apps don't need access to /mnt/user paths directly. |
| 1207 | // Without FUSE however, apps need /mnt/user access so /mnt/user in init.rc is 0755 until here |
Zim | 4dd4709 | 2020-01-29 02:44:46 +0000 | [diff] [blame] | 1208 | auto result = PrepareDir("/mnt/user", 0750, AID_ROOT, AID_MEDIA_RW); |
Zim | 1242be8 | 2020-01-22 18:22:29 +0000 | [diff] [blame] | 1209 | if (result != android::OK) { |
| 1210 | PLOG(ERROR) << "Failed to prepare directory /mnt/user"; |
| 1211 | return -1; |
| 1212 | } |
| 1213 | |
Zim | 06b0caf | 2020-01-05 02:11:47 +0000 | [diff] [blame] | 1214 | // Shell is neither AID_ROOT nor AID_EVERYBODY. Since it equally needs 'execute' access to |
| 1215 | // /mnt/user/0 to 'adb shell ls /sdcard' for instance, we set the uid bit of /mnt/user/0 to |
| 1216 | // AID_SHELL. This gives shell access along with apps running as group everybody (user 0 apps) |
| 1217 | // These bits should be consistent with what is set in zygote in |
| 1218 | // com_android_internal_os_Zygote#MountEmulatedStorage on volume bind mount during app fork |
Zim | 1242be8 | 2020-01-22 18:22:29 +0000 | [diff] [blame] | 1219 | result = PrepareDir(pre_fuse_path, 0710, user_id ? AID_ROOT : AID_SHELL, |
Zim | 06b0caf | 2020-01-05 02:11:47 +0000 | [diff] [blame] | 1220 | multiuser_get_uid(user_id, AID_EVERYBODY)); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1221 | if (result != android::OK) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1222 | PLOG(ERROR) << "Failed to prepare directory " << pre_fuse_path; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1223 | return -1; |
| 1224 | } |
| 1225 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1226 | result = PrepareDir(fuse_path, 0700, AID_ROOT, AID_ROOT); |
| 1227 | if (result != android::OK) { |
| 1228 | PLOG(ERROR) << "Failed to prepare directory " << fuse_path; |
| 1229 | return -1; |
| 1230 | } |
| 1231 | |
Zim | 26eec70 | 2020-01-31 16:00:58 +0000 | [diff] [blame] | 1232 | result = PrepareDir(pre_pass_through_path, 0710, AID_ROOT, AID_MEDIA_RW); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1233 | if (result != android::OK) { |
| 1234 | PLOG(ERROR) << "Failed to prepare directory " << pre_pass_through_path; |
| 1235 | return -1; |
| 1236 | } |
| 1237 | |
Zim | 26eec70 | 2020-01-31 16:00:58 +0000 | [diff] [blame] | 1238 | result = PrepareDir(pass_through_path, 0710, AID_ROOT, AID_MEDIA_RW); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1239 | if (result != android::OK) { |
| 1240 | PLOG(ERROR) << "Failed to prepare directory " << pass_through_path; |
| 1241 | return -1; |
| 1242 | } |
| 1243 | |
| 1244 | if (relative_upper_path == "emulated") { |
Zim | e5393d4 | 2019-11-15 11:44:12 +0000 | [diff] [blame] | 1245 | std::string linkpath(StringPrintf("/mnt/user/%d/self", user_id)); |
| 1246 | result = PrepareDir(linkpath, 0755, AID_ROOT, AID_ROOT); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1247 | if (result != android::OK) { |
Zim | e5393d4 | 2019-11-15 11:44:12 +0000 | [diff] [blame] | 1248 | PLOG(ERROR) << "Failed to prepare directory " << linkpath; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1249 | return -1; |
| 1250 | } |
Zim | e5393d4 | 2019-11-15 11:44:12 +0000 | [diff] [blame] | 1251 | linkpath += "/primary"; |
Zim | 53d16d3 | 2020-01-17 01:21:24 +0000 | [diff] [blame] | 1252 | Symlink("/storage/emulated/" + std::to_string(user_id), linkpath); |
Zim | aea1247 | 2020-01-08 11:09:47 +0000 | [diff] [blame] | 1253 | |
| 1254 | std::string pass_through_linkpath(StringPrintf("/mnt/pass_through/%d/self", user_id)); |
Zim | 26eec70 | 2020-01-31 16:00:58 +0000 | [diff] [blame] | 1255 | result = PrepareDir(pass_through_linkpath, 0710, AID_ROOT, AID_MEDIA_RW); |
Zim | aea1247 | 2020-01-08 11:09:47 +0000 | [diff] [blame] | 1256 | if (result != android::OK) { |
| 1257 | PLOG(ERROR) << "Failed to prepare directory " << pass_through_linkpath; |
| 1258 | return -1; |
| 1259 | } |
| 1260 | pass_through_linkpath += "/primary"; |
Zim | 53d16d3 | 2020-01-17 01:21:24 +0000 | [diff] [blame] | 1261 | Symlink("/storage/emulated/" + std::to_string(user_id), pass_through_linkpath); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1262 | } |
| 1263 | |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame] | 1264 | // Open fuse fd. |
| 1265 | fuse_fd->reset(open("/dev/fuse", O_RDWR | O_CLOEXEC)); |
| 1266 | if (fuse_fd->get() == -1) { |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1267 | PLOG(ERROR) << "Failed to open /dev/fuse"; |
| 1268 | return -1; |
| 1269 | } |
| 1270 | |
| 1271 | // Note: leaving out default_permissions since we don't want kernel to do lower filesystem |
| 1272 | // permission checks before routing to FUSE daemon. |
| 1273 | const auto opts = StringPrintf( |
| 1274 | "fd=%i," |
| 1275 | "rootmode=40000," |
| 1276 | "allow_other," |
| 1277 | "user_id=0,group_id=0,", |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame] | 1278 | fuse_fd->get()); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1279 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1280 | result = TEMP_FAILURE_RETRY(mount("/dev/fuse", fuse_path.c_str(), "fuse", |
| 1281 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_LAZYTIME, |
| 1282 | opts.c_str())); |
| 1283 | if (result != 0) { |
| 1284 | PLOG(ERROR) << "Failed to mount " << fuse_path; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1285 | return -errno; |
| 1286 | } |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 1287 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 1288 | if (IsFilesystemSupported("sdcardfs")) { |
| 1289 | std::string sdcardfs_path( |
| 1290 | StringPrintf("/mnt/runtime/full/%s", relative_upper_path.c_str())); |
| 1291 | |
| 1292 | LOG(INFO) << "Bind mounting " << sdcardfs_path << " to " << pass_through_path; |
| 1293 | return BindMount(sdcardfs_path, pass_through_path); |
| 1294 | } else { |
| 1295 | LOG(INFO) << "Bind mounting " << absolute_lower_path << " to " << pass_through_path; |
| 1296 | return BindMount(absolute_lower_path, pass_through_path); |
| 1297 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1298 | } |
| 1299 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 1300 | status_t UnmountUserFuse(userid_t user_id, const std::string& absolute_lower_path, |
| 1301 | const std::string& relative_upper_path) { |
| 1302 | std::string fuse_path(StringPrintf("/mnt/user/%d/%s", user_id, relative_upper_path.c_str())); |
| 1303 | std::string pass_through_path( |
| 1304 | StringPrintf("/mnt/pass_through/%d/%s", user_id, relative_upper_path.c_str())); |
| 1305 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1306 | // Best effort unmount pass_through path |
| 1307 | sSleepOnUnmount = false; |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 1308 | LOG(INFO) << "Unmounting pass_through_path " << pass_through_path; |
| 1309 | auto status = ForceUnmount(pass_through_path); |
| 1310 | if (status != android::OK) { |
| 1311 | LOG(ERROR) << "Failed to unmount " << pass_through_path; |
| 1312 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 1313 | rmdir(pass_through_path.c_str()); |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 1314 | |
| 1315 | LOG(INFO) << "Unmounting fuse path " << fuse_path; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1316 | android::status_t result = ForceUnmount(fuse_path); |
| 1317 | sSleepOnUnmount = true; |
| 1318 | if (result != android::OK) { |
| 1319 | // TODO(b/135341433): MNT_DETACH is needed for fuse because umount2 can fail with EBUSY. |
| 1320 | // Figure out why we get EBUSY and remove this special casing if possible. |
| 1321 | PLOG(ERROR) << "Failed to unmount. Trying MNT_DETACH " << fuse_path << " ..."; |
| 1322 | if (umount2(fuse_path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) && errno != EINVAL && |
| 1323 | errno != ENOENT) { |
| 1324 | PLOG(ERROR) << "Failed to unmount with MNT_DETACH " << fuse_path; |
| 1325 | return -errno; |
| 1326 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 1327 | result = android::OK; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1328 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 1329 | rmdir(fuse_path.c_str()); |
| 1330 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1331 | return result; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1332 | } |
| 1333 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1334 | status_t PrepareAndroidDirs(const std::string& volumeRoot) { |
| 1335 | std::string androidDir = volumeRoot + kAndroidDir; |
| 1336 | std::string androidDataDir = volumeRoot + kAppDataDir; |
| 1337 | std::string androidObbDir = volumeRoot + kAppObbDir; |
Zim | a13d81b | 2020-02-07 16:39:31 +0000 | [diff] [blame] | 1338 | std::string androidMediaDir = volumeRoot + kAppMediaDir; |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1339 | |
| 1340 | bool useSdcardFs = IsFilesystemSupported("sdcardfs"); |
| 1341 | |
| 1342 | if (fs_prepare_dir(androidDir.c_str(), 0771, AID_MEDIA_RW, AID_MEDIA_RW) != 0) { |
| 1343 | PLOG(ERROR) << "Failed to create " << androidDir; |
| 1344 | return -errno; |
| 1345 | } |
| 1346 | |
| 1347 | gid_t dataGid = useSdcardFs ? AID_MEDIA_RW : AID_EXT_DATA_RW; |
| 1348 | if (fs_prepare_dir(androidDataDir.c_str(), 0771, AID_MEDIA_RW, dataGid) != 0) { |
| 1349 | PLOG(ERROR) << "Failed to create " << androidDataDir; |
| 1350 | return -errno; |
| 1351 | } |
| 1352 | |
| 1353 | gid_t obbGid = useSdcardFs ? AID_MEDIA_RW : AID_EXT_OBB_RW; |
| 1354 | if (fs_prepare_dir(androidObbDir.c_str(), 0771, AID_MEDIA_RW, obbGid) != 0) { |
| 1355 | PLOG(ERROR) << "Failed to create " << androidObbDir; |
| 1356 | return -errno; |
| 1357 | } |
| 1358 | |
Zim | a13d81b | 2020-02-07 16:39:31 +0000 | [diff] [blame] | 1359 | if (fs_prepare_dir(androidMediaDir.c_str(), 0771, AID_MEDIA_RW, AID_MEDIA_RW) != 0) { |
| 1360 | PLOG(ERROR) << "Failed to create " << androidMediaDir; |
| 1361 | return -errno; |
| 1362 | } |
| 1363 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1364 | return OK; |
| 1365 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1366 | } // namespace vold |
| 1367 | } // namespace android |