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