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