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