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