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