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