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