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