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