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 | cfe3972 | 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> |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [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> |
| 39 | #include <sys/mount.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 40 | #include <sys/stat.h> |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 41 | #include <sys/statvfs.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 42 | #include <sys/sysmacros.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/wait.h> |
Paul Crowley | 747b421 | 2019-04-05 04:09:57 -0700 | [diff] [blame] | 45 | #include <unistd.h> |
Paul Crowley | cfe3972 | 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 | cfe3972 | 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 | cfe3972 | 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) { |
| 197 | if (::mount(source.c_str(), target.c_str(), "", MS_BIND, NULL)) { |
| 198 | PLOG(ERROR) << "Failed to bind mount " << source << " to " << target; |
| 199 | return -errno; |
| 200 | } |
| 201 | return OK; |
| 202 | } |
| 203 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 204 | bool FindValue(const std::string& raw, const std::string& key, std::string* value) { |
| 205 | auto qual = key + "=\""; |
Paul Crowley | 95abfa0 | 2019-02-05 15:33:34 -0800 | [diff] [blame] | 206 | size_t start = 0; |
| 207 | while (true) { |
| 208 | start = raw.find(qual, start); |
| 209 | if (start == std::string::npos) return false; |
| 210 | if (start == 0 || raw[start - 1] == ' ') { |
| 211 | break; |
| 212 | } |
| 213 | start += 1; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 214 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 215 | start += qual.length(); |
| 216 | |
| 217 | auto end = raw.find("\"", start); |
| 218 | if (end == std::string::npos) return false; |
| 219 | |
| 220 | *value = raw.substr(start, end - start); |
| 221 | return true; |
| 222 | } |
| 223 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 224 | static status_t readMetadata(const std::string& path, std::string* fsType, std::string* fsUuid, |
| 225 | std::string* fsLabel, bool untrusted) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 226 | fsType->clear(); |
| 227 | fsUuid->clear(); |
| 228 | fsLabel->clear(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 229 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 230 | std::vector<std::string> cmd; |
| 231 | cmd.push_back(kBlkidPath); |
| 232 | cmd.push_back("-c"); |
| 233 | cmd.push_back("/dev/null"); |
Jeff Sharkey | eddf9bd | 2015-08-12 16:04:35 -0700 | [diff] [blame] | 234 | cmd.push_back("-s"); |
| 235 | cmd.push_back("TYPE"); |
| 236 | cmd.push_back("-s"); |
| 237 | cmd.push_back("UUID"); |
| 238 | cmd.push_back("-s"); |
| 239 | cmd.push_back("LABEL"); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 240 | cmd.push_back(path); |
| 241 | |
| 242 | std::vector<std::string> output; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 243 | status_t res = ForkExecvp(cmd, &output, untrusted ? sBlkidUntrustedContext : sBlkidContext); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 244 | if (res != OK) { |
| 245 | LOG(WARNING) << "blkid failed to identify " << path; |
| 246 | return res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 249 | for (const auto& line : output) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 250 | // Extract values from blkid output, if defined |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 251 | FindValue(line, "TYPE", fsType); |
| 252 | FindValue(line, "UUID", fsUuid); |
| 253 | FindValue(line, "LABEL", fsLabel); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 256 | return OK; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 259 | status_t ReadMetadata(const std::string& path, std::string* fsType, std::string* fsUuid, |
| 260 | std::string* fsLabel) { |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 261 | return readMetadata(path, fsType, fsUuid, fsLabel, false); |
| 262 | } |
| 263 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 264 | status_t ReadMetadataUntrusted(const std::string& path, std::string* fsType, std::string* fsUuid, |
| 265 | std::string* fsLabel) { |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 266 | return readMetadata(path, fsType, fsUuid, fsLabel, true); |
| 267 | } |
| 268 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 269 | static std::vector<const char*> ConvertToArgv(const std::vector<std::string>& args) { |
| 270 | std::vector<const char*> argv; |
| 271 | argv.reserve(args.size() + 1); |
| 272 | for (const auto& arg : args) { |
| 273 | if (argv.empty()) { |
| 274 | LOG(DEBUG) << arg; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 275 | } else { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 276 | LOG(DEBUG) << " " << arg; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 277 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 278 | argv.emplace_back(arg.data()); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 279 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 280 | argv.emplace_back(nullptr); |
| 281 | return argv; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 284 | static status_t ReadLinesFromFdAndLog(std::vector<std::string>* output, |
| 285 | android::base::unique_fd ufd) { |
| 286 | 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] | 287 | if (!fp) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 288 | PLOG(ERROR) << "fdopen in ReadLinesFromFdAndLog"; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 289 | return -errno; |
| 290 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 291 | if (output) output->clear(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 292 | char line[1024]; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 293 | while (fgets(line, sizeof(line), fp.get()) != nullptr) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 294 | LOG(DEBUG) << line; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 295 | if (output) output->emplace_back(line); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 296 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 297 | return OK; |
| 298 | } |
| 299 | |
| 300 | status_t ForkExecvp(const std::vector<std::string>& args, std::vector<std::string>* output, |
| 301 | security_context_t context) { |
| 302 | auto argv = ConvertToArgv(args); |
| 303 | |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 304 | android::base::unique_fd pipe_read, pipe_write; |
| 305 | if (!android::base::Pipe(&pipe_read, &pipe_write)) { |
| 306 | PLOG(ERROR) << "Pipe in ForkExecvp"; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 307 | return -errno; |
| 308 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 309 | |
| 310 | pid_t pid = fork(); |
| 311 | if (pid == 0) { |
| 312 | if (context) { |
| 313 | if (setexeccon(context)) { |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 314 | LOG(ERROR) << "Failed to setexeccon in ForkExecvp"; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 315 | abort(); |
| 316 | } |
| 317 | } |
| 318 | pipe_read.reset(); |
Paul Crowley | be857bf | 2018-12-07 12:23:25 -0800 | [diff] [blame] | 319 | if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) { |
| 320 | PLOG(ERROR) << "dup2 in ForkExecvp"; |
| 321 | _exit(EXIT_FAILURE); |
| 322 | } |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 323 | pipe_write.reset(); |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 324 | execvp(argv[0], const_cast<char**>(argv.data())); |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 325 | PLOG(ERROR) << "exec in ForkExecvp"; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 326 | _exit(EXIT_FAILURE); |
| 327 | } |
| 328 | if (pid == -1) { |
| 329 | PLOG(ERROR) << "fork in ForkExecvp"; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 330 | return -errno; |
| 331 | } |
| 332 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 333 | pipe_write.reset(); |
| 334 | auto st = ReadLinesFromFdAndLog(output, std::move(pipe_read)); |
| 335 | if (st != 0) return st; |
| 336 | |
| 337 | int status; |
| 338 | if (waitpid(pid, &status, 0) == -1) { |
| 339 | PLOG(ERROR) << "waitpid in ForkExecvp"; |
| 340 | return -errno; |
| 341 | } |
| 342 | if (!WIFEXITED(status)) { |
| 343 | LOG(ERROR) << "Process did not exit normally, status: " << status; |
| 344 | return -ECHILD; |
| 345 | } |
| 346 | if (WEXITSTATUS(status)) { |
| 347 | LOG(ERROR) << "Process exited with code: " << WEXITSTATUS(status); |
| 348 | return WEXITSTATUS(status); |
| 349 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 350 | return OK; |
| 351 | } |
| 352 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 353 | pid_t ForkExecvpAsync(const std::vector<std::string>& args) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 354 | auto argv = ConvertToArgv(args); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 355 | |
| 356 | pid_t pid = fork(); |
| 357 | if (pid == 0) { |
| 358 | close(STDIN_FILENO); |
| 359 | close(STDOUT_FILENO); |
| 360 | close(STDERR_FILENO); |
| 361 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 362 | execvp(argv[0], const_cast<char**>(argv.data())); |
| 363 | PLOG(ERROR) << "exec in ForkExecvpAsync"; |
| 364 | _exit(EXIT_FAILURE); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 365 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 366 | if (pid == -1) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 367 | PLOG(ERROR) << "fork in ForkExecvpAsync"; |
| 368 | return -1; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 369 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 370 | return pid; |
| 371 | } |
| 372 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 373 | status_t ReadRandomBytes(size_t bytes, std::string& out) { |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 374 | out.resize(bytes); |
| 375 | return ReadRandomBytes(bytes, &out[0]); |
| 376 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 377 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 378 | status_t ReadRandomBytes(size_t bytes, char* buf) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 379 | int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 380 | if (fd == -1) { |
| 381 | return -errno; |
| 382 | } |
| 383 | |
Eric Biggers | 0ef7bfd | 2019-01-16 13:05:34 -0800 | [diff] [blame] | 384 | ssize_t n; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 385 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], bytes))) > 0) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 386 | bytes -= n; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 387 | buf += n; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 388 | } |
Elliott Hughes | a623108 | 2015-05-15 18:34:24 -0700 | [diff] [blame] | 389 | close(fd); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 390 | |
| 391 | if (bytes == 0) { |
| 392 | return OK; |
| 393 | } else { |
| 394 | return -EIO; |
| 395 | } |
| 396 | } |
| 397 | |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 398 | status_t GenerateRandomUuid(std::string& out) { |
| 399 | status_t res = ReadRandomBytes(16, out); |
| 400 | if (res == OK) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 401 | out[6] &= 0x0f; /* clear version */ |
| 402 | out[6] |= 0x40; /* set to version 4 */ |
| 403 | out[8] &= 0x3f; /* clear variant */ |
| 404 | out[8] |= 0x80; /* set to IETF variant */ |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 405 | } |
| 406 | return res; |
| 407 | } |
| 408 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 409 | status_t HexToStr(const std::string& hex, std::string& str) { |
| 410 | str.clear(); |
| 411 | bool even = true; |
| 412 | char cur = 0; |
| 413 | for (size_t i = 0; i < hex.size(); i++) { |
| 414 | int val = 0; |
| 415 | switch (hex[i]) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 416 | // clang-format off |
| 417 | case ' ': case '-': case ':': continue; |
| 418 | case 'f': case 'F': val = 15; break; |
| 419 | case 'e': case 'E': val = 14; break; |
| 420 | case 'd': case 'D': val = 13; break; |
| 421 | case 'c': case 'C': val = 12; break; |
| 422 | case 'b': case 'B': val = 11; break; |
| 423 | case 'a': case 'A': val = 10; break; |
| 424 | case '9': val = 9; break; |
| 425 | case '8': val = 8; break; |
| 426 | case '7': val = 7; break; |
| 427 | case '6': val = 6; break; |
| 428 | case '5': val = 5; break; |
| 429 | case '4': val = 4; break; |
| 430 | case '3': val = 3; break; |
| 431 | case '2': val = 2; break; |
| 432 | case '1': val = 1; break; |
| 433 | case '0': val = 0; break; |
| 434 | default: return -EINVAL; |
| 435 | // clang-format on |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | if (even) { |
| 439 | cur = val << 4; |
| 440 | } else { |
| 441 | cur += val; |
| 442 | str.push_back(cur); |
| 443 | cur = 0; |
| 444 | } |
| 445 | even = !even; |
| 446 | } |
| 447 | return even ? OK : -EINVAL; |
| 448 | } |
| 449 | |
| 450 | static const char* kLookup = "0123456789abcdef"; |
| 451 | |
| 452 | status_t StrToHex(const std::string& str, std::string& hex) { |
| 453 | hex.clear(); |
| 454 | for (size_t i = 0; i < str.size(); i++) { |
Jeff Sharkey | ef36975 | 2015-04-29 15:57:48 -0700 | [diff] [blame] | 455 | hex.push_back(kLookup[(str[i] & 0xF0) >> 4]); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 456 | hex.push_back(kLookup[str[i] & 0x0F]); |
| 457 | } |
| 458 | return OK; |
| 459 | } |
| 460 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 461 | status_t StrToHex(const KeyBuffer& str, KeyBuffer& hex) { |
| 462 | hex.clear(); |
| 463 | for (size_t i = 0; i < str.size(); i++) { |
| 464 | hex.push_back(kLookup[(str.data()[i] & 0xF0) >> 4]); |
| 465 | hex.push_back(kLookup[str.data()[i] & 0x0F]); |
| 466 | } |
| 467 | return OK; |
| 468 | } |
| 469 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 470 | status_t NormalizeHex(const std::string& in, std::string& out) { |
| 471 | std::string tmp; |
| 472 | if (HexToStr(in, tmp)) { |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | return StrToHex(tmp, out); |
| 476 | } |
| 477 | |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 478 | status_t GetBlockDevSize(int fd, uint64_t* size) { |
| 479 | if (ioctl(fd, BLKGETSIZE64, size)) { |
| 480 | return -errno; |
| 481 | } |
| 482 | |
| 483 | return OK; |
| 484 | } |
| 485 | |
| 486 | status_t GetBlockDevSize(const std::string& path, uint64_t* size) { |
| 487 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 488 | status_t res = OK; |
| 489 | |
| 490 | if (fd < 0) { |
| 491 | return -errno; |
| 492 | } |
| 493 | |
| 494 | res = GetBlockDevSize(fd, size); |
| 495 | |
| 496 | close(fd); |
| 497 | |
| 498 | return res; |
| 499 | } |
| 500 | |
| 501 | status_t GetBlockDev512Sectors(const std::string& path, uint64_t* nr_sec) { |
| 502 | uint64_t size; |
| 503 | status_t res = GetBlockDevSize(path, &size); |
| 504 | |
| 505 | if (res != OK) { |
| 506 | return res; |
| 507 | } |
| 508 | |
| 509 | *nr_sec = size / 512; |
| 510 | |
| 511 | return OK; |
| 512 | } |
| 513 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 514 | uint64_t GetFreeBytes(const std::string& path) { |
| 515 | struct statvfs sb; |
| 516 | if (statvfs(path.c_str(), &sb) == 0) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 517 | return (uint64_t)sb.f_bavail * sb.f_frsize; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 518 | } else { |
| 519 | return -1; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | // TODO: borrowed from frameworks/native/libs/diskusage/ which should |
| 524 | // eventually be migrated into system/ |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 525 | static int64_t stat_size(struct stat* s) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 526 | int64_t blksize = s->st_blksize; |
| 527 | // count actual blocks used instead of nominal file size |
| 528 | int64_t size = s->st_blocks * 512; |
| 529 | |
| 530 | if (blksize) { |
| 531 | /* round up to filesystem block size */ |
| 532 | size = (size + blksize - 1) & (~(blksize - 1)); |
| 533 | } |
| 534 | |
| 535 | return size; |
| 536 | } |
| 537 | |
| 538 | // TODO: borrowed from frameworks/native/libs/diskusage/ which should |
| 539 | // eventually be migrated into system/ |
| 540 | int64_t calculate_dir_size(int dfd) { |
| 541 | int64_t size = 0; |
| 542 | struct stat s; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 543 | DIR* d; |
| 544 | struct dirent* de; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 545 | |
| 546 | d = fdopendir(dfd); |
| 547 | if (d == NULL) { |
| 548 | close(dfd); |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | while ((de = readdir(d))) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 553 | const char* name = de->d_name; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 554 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 555 | size += stat_size(&s); |
| 556 | } |
| 557 | if (de->d_type == DT_DIR) { |
| 558 | int subfd; |
| 559 | |
| 560 | /* always skip "." and ".." */ |
| 561 | if (name[0] == '.') { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 562 | if (name[1] == 0) continue; |
| 563 | if ((name[1] == '.') && (name[2] == 0)) continue; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 566 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 567 | if (subfd >= 0) { |
| 568 | size += calculate_dir_size(subfd); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | closedir(d); |
| 573 | return size; |
| 574 | } |
| 575 | |
| 576 | uint64_t GetTreeBytes(const std::string& path) { |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 577 | int dirfd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 578 | if (dirfd < 0) { |
| 579 | PLOG(WARNING) << "Failed to open " << path; |
| 580 | return -1; |
| 581 | } else { |
Josh Gao | 72fb1a6 | 2018-05-29 19:05:16 -0700 | [diff] [blame] | 582 | return calculate_dir_size(dirfd); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 586 | bool IsFilesystemSupported(const std::string& fsType) { |
| 587 | std::string supported; |
| 588 | if (!ReadFileToString(kProcFilesystems, &supported)) { |
| 589 | PLOG(ERROR) << "Failed to read supported filesystems"; |
| 590 | return false; |
| 591 | } |
| 592 | return supported.find(fsType + "\n") != std::string::npos; |
| 593 | } |
| 594 | |
| 595 | status_t WipeBlockDevice(const std::string& path) { |
| 596 | status_t res = -1; |
| 597 | const char* c_path = path.c_str(); |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 598 | uint64_t range[2] = {0, 0}; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 599 | |
| 600 | int fd = TEMP_FAILURE_RETRY(open(c_path, O_RDWR | O_CLOEXEC)); |
| 601 | if (fd == -1) { |
| 602 | PLOG(ERROR) << "Failed to open " << path; |
| 603 | goto done; |
| 604 | } |
| 605 | |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 606 | if (GetBlockDevSize(fd, &range[1]) != OK) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 607 | PLOG(ERROR) << "Failed to determine size of " << path; |
| 608 | goto done; |
| 609 | } |
| 610 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 611 | LOG(INFO) << "About to discard " << range[1] << " on " << path; |
| 612 | if (ioctl(fd, BLKDISCARD, &range) == 0) { |
| 613 | LOG(INFO) << "Discard success on " << path; |
| 614 | res = 0; |
| 615 | } else { |
| 616 | PLOG(ERROR) << "Discard failure on " << path; |
| 617 | } |
| 618 | |
| 619 | done: |
| 620 | close(fd); |
| 621 | return res; |
| 622 | } |
| 623 | |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 624 | static bool isValidFilename(const std::string& name) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 625 | if (name.empty() || (name == ".") || (name == "..") || (name.find('/') != std::string::npos)) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 626 | return false; |
| 627 | } else { |
| 628 | return true; |
| 629 | } |
| 630 | } |
| 631 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 632 | std::string BuildKeyPath(const std::string& partGuid) { |
| 633 | return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str()); |
| 634 | } |
| 635 | |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 636 | std::string BuildDataSystemLegacyPath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 637 | return StringPrintf("%s/system/users/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 638 | } |
| 639 | |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 640 | std::string BuildDataSystemCePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 641 | return StringPrintf("%s/system_ce/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | std::string BuildDataSystemDePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 645 | return StringPrintf("%s/system_de/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 648 | std::string BuildDataMiscLegacyPath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 649 | return StringPrintf("%s/misc/user/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 650 | } |
| 651 | |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 652 | std::string BuildDataMiscCePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 653 | return StringPrintf("%s/misc_ce/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | std::string BuildDataMiscDePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 657 | return StringPrintf("%s/misc_de/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Calin Juravle | 79f55a4 | 2016-02-17 20:14:46 +0000 | [diff] [blame] | 660 | // Keep in sync with installd (frameworks/native/cmds/installd/utils.h) |
| 661 | std::string BuildDataProfilesDePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 662 | return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath("").c_str(), userId); |
Calin Juravle | 79f55a4 | 2016-02-17 20:14:46 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 665 | std::string BuildDataVendorCePath(userid_t userId) { |
| 666 | return StringPrintf("%s/vendor_ce/%u", BuildDataPath("").c_str(), userId); |
| 667 | } |
| 668 | |
| 669 | std::string BuildDataVendorDePath(userid_t userId) { |
| 670 | return StringPrintf("%s/vendor_de/%u", BuildDataPath("").c_str(), userId); |
| 671 | } |
| 672 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 673 | std::string BuildDataPath(const std::string& volumeUuid) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 674 | // TODO: unify with installd path generation logic |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 675 | if (volumeUuid.empty()) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 676 | return "/data"; |
| 677 | } else { |
| 678 | CHECK(isValidFilename(volumeUuid)); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 679 | return StringPrintf("/mnt/expand/%s", volumeUuid.c_str()); |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 683 | std::string BuildDataMediaCePath(const std::string& volumeUuid, userid_t userId) { |
Jeff Sharkey | fc505c3 | 2015-12-07 17:27:01 -0700 | [diff] [blame] | 684 | // TODO: unify with installd path generation logic |
| 685 | std::string data(BuildDataPath(volumeUuid)); |
| 686 | return StringPrintf("%s/media/%u", data.c_str(), userId); |
| 687 | } |
| 688 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 689 | std::string BuildDataUserCePath(const std::string& volumeUuid, userid_t userId) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 690 | // TODO: unify with installd path generation logic |
| 691 | std::string data(BuildDataPath(volumeUuid)); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 692 | if (volumeUuid.empty() && userId == 0) { |
cjbao | eb50114 | 2017-04-12 00:09:00 +0800 | [diff] [blame] | 693 | std::string legacy = StringPrintf("%s/data", data.c_str()); |
| 694 | struct stat sb; |
| 695 | if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 696 | /* /data/data is dir, return /data/data for legacy system */ |
| 697 | return legacy; |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 698 | } |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 699 | } |
cjbao | eb50114 | 2017-04-12 00:09:00 +0800 | [diff] [blame] | 700 | return StringPrintf("%s/user/%u", data.c_str(), userId); |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 703 | std::string BuildDataUserDePath(const std::string& volumeUuid, userid_t userId) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 704 | // TODO: unify with installd path generation logic |
| 705 | std::string data(BuildDataPath(volumeUuid)); |
| 706 | return StringPrintf("%s/user_de/%u", data.c_str(), userId); |
| 707 | } |
| 708 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 709 | dev_t GetDevice(const std::string& path) { |
| 710 | struct stat sb; |
| 711 | if (stat(path.c_str(), &sb)) { |
| 712 | PLOG(WARNING) << "Failed to stat " << path; |
| 713 | return 0; |
| 714 | } else { |
| 715 | return sb.st_dev; |
| 716 | } |
| 717 | } |
| 718 | |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 719 | status_t RestoreconRecursive(const std::string& path) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 720 | LOG(DEBUG) << "Starting restorecon of " << path; |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 721 | |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 722 | static constexpr const char* kRestoreconString = "selinux.restorecon_recursive"; |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 723 | |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 724 | android::base::SetProperty(kRestoreconString, ""); |
| 725 | android::base::SetProperty(kRestoreconString, path); |
| 726 | |
| 727 | android::base::WaitForProperty(kRestoreconString, path); |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 728 | |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 729 | LOG(DEBUG) << "Finished restorecon of " << path; |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 730 | return OK; |
| 731 | } |
| 732 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 733 | bool Readlinkat(int dirfd, const std::string& path, std::string* result) { |
| 734 | // Shamelessly borrowed from android::base::Readlink() |
| 735 | result->clear(); |
| 736 | |
| 737 | // Most Linux file systems (ext2 and ext4, say) limit symbolic links to |
| 738 | // 4095 bytes. Since we'll copy out into the string anyway, it doesn't |
| 739 | // waste memory to just start there. We add 1 so that we can recognize |
| 740 | // whether it actually fit (rather than being truncated to 4095). |
| 741 | std::vector<char> buf(4095 + 1); |
| 742 | while (true) { |
| 743 | ssize_t size = readlinkat(dirfd, path.c_str(), &buf[0], buf.size()); |
| 744 | // Unrecoverable error? |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 745 | if (size == -1) return false; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 746 | // It fit! (If size == buf.size(), it may have been truncated.) |
| 747 | if (static_cast<size_t>(size) < buf.size()) { |
| 748 | result->assign(&buf[0], size); |
| 749 | return true; |
| 750 | } |
| 751 | // Double our buffer and try again. |
| 752 | buf.resize(buf.size() * 2); |
Daichi Hirono | 10d3488 | 2016-01-29 14:33:51 +0900 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 756 | bool IsRunningInEmulator() { |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 757 | return android::base::GetBoolProperty("ro.kernel.qemu", false); |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 758 | } |
| 759 | |
Sudheer Shanka | 295fb24 | 2019-01-16 23:04:07 -0800 | [diff] [blame] | 760 | static status_t findMountPointsWithPrefix(const std::string& prefix, |
| 761 | std::list<std::string>& mountPoints) { |
| 762 | // Add a trailing slash if the client didn't provide one so that we don't match /foo/barbaz |
| 763 | // when the prefix is /foo/bar |
| 764 | std::string prefixWithSlash(prefix); |
| 765 | if (prefix.back() != '/') { |
| 766 | android::base::StringAppendF(&prefixWithSlash, "/"); |
| 767 | } |
| 768 | |
| 769 | std::unique_ptr<FILE, int (*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent); |
| 770 | if (!mnts) { |
| 771 | PLOG(ERROR) << "Unable to open /proc/mounts"; |
| 772 | return -errno; |
| 773 | } |
| 774 | |
| 775 | // Some volumes can be stacked on each other, so force unmount in |
| 776 | // reverse order to give us the best chance of success. |
| 777 | struct mntent* mnt; // getmntent returns a thread local, so it's safe. |
| 778 | while ((mnt = getmntent(mnts.get())) != nullptr) { |
| 779 | auto mountPoint = std::string(mnt->mnt_dir) + "/"; |
| 780 | if (android::base::StartsWith(mountPoint, prefixWithSlash)) { |
| 781 | mountPoints.push_front(mountPoint); |
| 782 | } |
| 783 | } |
| 784 | return OK; |
| 785 | } |
| 786 | |
| 787 | // Unmount all mountpoints that start with prefix. prefix itself doesn't need to be a mountpoint. |
| 788 | status_t UnmountTreeWithPrefix(const std::string& prefix) { |
| 789 | std::list<std::string> toUnmount; |
| 790 | status_t result = findMountPointsWithPrefix(prefix, toUnmount); |
| 791 | if (result < 0) { |
| 792 | return result; |
| 793 | } |
| 794 | for (const auto& path : toUnmount) { |
| 795 | if (umount2(path.c_str(), MNT_DETACH)) { |
| 796 | PLOG(ERROR) << "Failed to unmount " << path; |
| 797 | result = -errno; |
| 798 | } |
| 799 | } |
| 800 | return result; |
| 801 | } |
| 802 | |
| 803 | status_t UnmountTree(const std::string& mountPoint) { |
| 804 | if (umount2(mountPoint.c_str(), MNT_DETACH)) { |
| 805 | PLOG(ERROR) << "Failed to unmount " << mountPoint; |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 806 | return -errno; |
| 807 | } |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 808 | return OK; |
| 809 | } |
| 810 | |
Paul Crowley | cfe3972 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 811 | // TODO(118708649): fix duplication with init/util.h |
| 812 | status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout) { |
| 813 | android::base::Timer t; |
| 814 | while (t.duration() < timeout) { |
| 815 | struct stat sb; |
| 816 | if (stat(filename, &sb) != -1) { |
| 817 | LOG(INFO) << "wait for '" << filename << "' took " << t; |
| 818 | return 0; |
| 819 | } |
| 820 | std::this_thread::sleep_for(10ms); |
| 821 | } |
| 822 | LOG(WARNING) << "wait for '" << filename << "' timed out and took " << t; |
| 823 | return -1; |
| 824 | } |
| 825 | |
Paul Crowley | 621d9b9 | 2018-12-07 15:36:09 -0800 | [diff] [blame] | 826 | bool FsyncDirectory(const std::string& dirname) { |
| 827 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dirname.c_str(), O_RDONLY | O_CLOEXEC))); |
| 828 | if (fd == -1) { |
| 829 | PLOG(ERROR) << "Failed to open " << dirname; |
| 830 | return false; |
| 831 | } |
| 832 | if (fsync(fd) == -1) { |
| 833 | if (errno == EROFS || errno == EINVAL) { |
| 834 | PLOG(WARNING) << "Skip fsync " << dirname |
| 835 | << " on a file system does not support synchronization"; |
| 836 | } else { |
| 837 | PLOG(ERROR) << "Failed to fsync " << dirname; |
| 838 | return false; |
| 839 | } |
| 840 | } |
| 841 | return true; |
| 842 | } |
| 843 | |
Tommy Chiu | 97466cb | 2019-03-26 17:18:09 +0800 | [diff] [blame] | 844 | bool writeStringToFile(const std::string& payload, const std::string& filename) { |
| 845 | android::base::unique_fd fd(TEMP_FAILURE_RETRY( |
| 846 | open(filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0666))); |
| 847 | if (fd == -1) { |
| 848 | PLOG(ERROR) << "Failed to open " << filename; |
| 849 | return false; |
| 850 | } |
| 851 | if (!android::base::WriteStringToFd(payload, fd)) { |
| 852 | PLOG(ERROR) << "Failed to write to " << filename; |
| 853 | unlink(filename.c_str()); |
| 854 | return false; |
| 855 | } |
| 856 | // fsync as close won't guarantee flush data |
| 857 | // see close(2), fsync(2) and b/68901441 |
| 858 | if (fsync(fd) == -1) { |
| 859 | if (errno == EROFS || errno == EINVAL) { |
| 860 | PLOG(WARNING) << "Skip fsync " << filename |
| 861 | << " on a file system does not support synchronization"; |
| 862 | } else { |
| 863 | PLOG(ERROR) << "Failed to fsync " << filename; |
| 864 | unlink(filename.c_str()); |
| 865 | return false; |
| 866 | } |
| 867 | } |
| 868 | return true; |
| 869 | } |
| 870 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 871 | } // namespace vold |
| 872 | } // namespace android |