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